1

I've seen lot's of answers to similar questions, but none that I can parse with limited PHP chops. Simply, I want to create a quick list of objects, then loop through that array and access the properties of each object. In javascript it's very simple:

var arrayOfObjects = [
{'color': 'red', 'size': 10},
{'color': 'blue', 'size': 4},
{'color': 'green', 'size': 6}
];
for(var i=0; i<arrayOfObjects.length; i++) {
console.log(arrayOfObjects[i]['color']);
console.log(arrayOfObjects[i]['size']);
}

I'm using PHP 5.3, and here's my first ugly stab at it:

$array_of_objects = array();
$object = new stdClass();
$object->color = 'red';
$object->size = 10;
array_push($array_of_objects, $object);
$object2 = new stdClass();
$object2->color = 'green';
$object2->size = 4;
array_push($array_of_objects, $object2);

This looks terrible.

2
  • ... with a modern version of PHP you can write something very, very similar to the JavaScript. Though if I'm creating an object it's normally because it should have a specific structure and purpose so I'd very rarely use an anonymous object. Commented May 11, 2016 at 15:40
  • I think you can use dirty-trick like this: $objects = array( (object)array('color'=>'red', 'size'=>'none'), (object)array('color'=>'blue', 'size'=>100) ); . Just readed inside the php-doc-comments. Commented May 11, 2016 at 15:50

4 Answers 4

1
<?php
$arrayOfObjects = array(
    (object)array('color' => 'red', 'size' => 10),
    (object)array('color' => 'blue', 'size' => 4),
    (object)array('color' => 'green', 'size' => 6),
);


foreach($arrayOfObjects as $obj) {
    echo $obj->color;
    echo $obj->size;
}

PHP arrays can have string indexes as well. Would this work out for your needs?

<?php
$arrayOfObjects = array(
    array('color' => 'red', 'size' => 10),
    array('color' => 'blue', 'size' => 4),
    array('color' => 'green', 'size' => 6),
);


foreach($arrayOfObjects as $obj) {
    echo $obj['color'];
    echo $obj['size'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

You can be even "lazier" (in a good way) when casting the arrays to objects : e.g. (object) ['color' => 'red', 'size' => 10]
@CD001 This won't work for PHP 5.3, which OP is using. Totally relevant for anyone with PHP 5.4+ though.
The string indexes is exactly what I need, I really don't need them as objects.
1

JSON is almost like the JavaScript declaration. Just make sure to use double-quotes ":

$array_of_objects = json_decode('[
{"color": "red", "size": 10},
{"color": "blue", "size": 4},
{"color": "green", "size": 6}
]');

foreach($array_of_objects as $obj) {
    echo $obj->color;
    echo $obj->size;
}

2 Comments

I like this solution too, but I'm trying to stick with native PHP for the moment.
OK, especially since you don't need objects it's far easier.
0

How about you create a class with two properties $color and $Size ? instantiate an object and push it to an array; Try this :

<?php

$Array = array();
$YourObject = new YourClass();
$YourObject->color = "red";
$YourObject->size = "10";

array_push($Array, $YourObject);


$YourObject = new YourClass();
$YourObject->color = "blue";
$YourObject->size = "4";

array_push($Array, $YourObject);


$YourObject = new YourClass();
$YourObject->color = "green";
$YourObject->size = "6";

array_push($Array, $YourObject);

print "<pre>";
print_r($Array);
print "</pre>";


class YourClass {
    public $color;
    public $size;
}
?>

Comments

0

Use a foreach loop to loop through the array and display it's member object's properties.

Here's the reference:

So your code should be like this:

$array_of_objects = array();
$object = new stdClass();
$object->color = 'red';
$object->size = 10;
array_push($array_of_objects, $object);
$object2 = new stdClass();
$object2->color = 'green';
$object2->size = 4;
array_push($array_of_objects, $object2);

foreach($array_of_objects as $object){
    echo "Color: " . $object->color . ", Size: " . $object->size . "<br />";
}

Output:

Color: red, Size: 10
Color: green, Size: 4

Sidenote: You can do var_dump($array_of_objects); to see the complete array structure.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.