Include new items into Objects (PHP)
As you see below, we have price
and name
properties for each Object row. I want to include one more property, category
, into each row. So, tricky question here: how can I do it?
stdClass Object
(
[0] => stdClass Object
(
[price] => 12.99
[name] => Million Dollar Baby (Two-Disc Widescreen)
)
[1开发者_开发百科] => stdClass Object
(
[price] => 599.95
[name] => Screets Kiddiebank Experience
)
[2] => stdClass Object
(
[price] => 1999.00
[name] => Screets Kiddiebank Unlimited
)
)
Is this what you are loooking for?
<?php
$p1 = new stdClass();
$p1->price = 12.99;
$p1->name = "Million Dollar Baby (Two-Disc Widescreen)";
$p2 = new stdClass();
$p2->price = 599.95;
$p2->name = "Screets Kiddiebank Experience";
$p3 = new stdClass();
$p3->price = 1999.00;
$p3->name = "Screets Kiddiebank Unlimited";
$products = array($p1, $p2, $p3);
foreach($products as $product) {
$product->category = "Some category";
}
var_dump($products);
?>
Do you mean something like this?
$objects->{0}->category = 'foo';
$objects->{1}->category = 'bar';
$objects->{2}->category = 'foo';
精彩评论