Level 6
What does $products contain?
1 like
foreach($products as $product)
{
$refer = 'book';
$refers = array();
$refers['refer '] =$refer ;
array_push($product, $refers);
return $product;
}
So the contents of the $products Collection are instances of stdClass and not arrays.
You can set directly to an stdClass by assigning an attribute:
foreach($products as $product)
{
$refer = 'book';
$refers = array();
$refers['refer'] = $refer ;
// array_push($product, $refers);
$product->refers = $refers;
return $product;
}
Also, are you meaning to return at the end of the foreach block? This will return the first $product object rather than iterating over each element.
Please or to participate in this conversation.