I have array converted into stdClass object like this,
stdClass Object
(
[1339697186] => stdClass Object
(
[1403873546800880] => stdClass Object
(
[quantity_request] => 2
[time_created] => 1339697190
[variant] => stdClass Object
(
[0] => 1403873546800887
)
)
)
[1339697196] => stdClass Object
(
[1403873546800880] => stdClass Object
(
[quantity_request] => 1
[time_created] => 1339697196
[variant] => stdClass Object
(
[0] => 1403889656952419
)
)
)
)
So if I want to get [quantity_request] of each item, I will loop twice to get the answer,
foreach ($items as $key => $item)
{
foreach ($item as $code => $item)
{
echo $item->quantity_request;
}
}
I want to know if there is a way to get the answer like this below without looping the object array twice?
foreach ($items as $key => $item)
{
# Get the product code of this item.
$code = $cart->search_code($key);
echo $item->$code->quantity_request;
}
error:
Fatal error: Cannot use object of type stdClass as array in…
I have a method in a class to get the code (sub key) from the content of the object array.
public function search_code($key)
{
# Get this item.
$item = $this->content[$key];
# Get this item's sub key, which is the code of the product.
$subkeys = array_keys($item);
# Get the first item from the array.
$code = $subkeys[0];
# Return the sub key which is the code of the product.
return $code;
}
Yes, that I know of :
I hope I answer your question, didn’t understand well at 100%.
EDIT