How to use array and object-array in PHP



There is Array in PHP, and there is also object. We can simply make array that each member contains a value such as string or integer or we can also store object as the value of each array member.

This is the example that I experimented with:

<?php

echo "PHP Array<br>";

$myarray = array();

array_push($myarray, "123");
array_push($myarray, "hello");
array_push($myarray, "yes");

var_dump($myarray);

echo "PHP Object Array<br>";

$myobjectarray = array();

$object1 = new \stdClass();
$object1->name = "Jack";
$object1->age = 25;

$object2 = new \stdClass();
$object2->name = "Roy";
$object2->age = 20;

$object3 = new \stdClass();
$object3->name = "Andy";
$object3->age = 22;

array_push($myobjectarray, $object1);
array_push($myobjectarray, $object2);
array_push($myobjectarray, $object3);

var_dump($myobjectarray);

Then the output will be this:

PHP Array
array(3) { [0]=> string(3) "123" [1]=> string(5) "hello" [2]=> string(3) "yes" } 

PHP Object Array
array(3) { [0]=> object(stdClass)#1 (2) { ["name"]=> string(4) "Jack" ["age"]=> int(25) } [1]=> object(stdClass)#2 (2) { ["name"]=> string(3) "Roy" ["age"]=> int(20) } [2]=> object(stdClass)#3 (2) { ["name"]=> string(4) "Andy" ["age"]=> int(22) } }
loading...

Leave a Reply

Your email address will not be published. Required fields are marked *