I am getting the Message: Invalid argument supplied for foreach()
when I run the following code:
foreach($combination as $combi){
var_dump($combi);
$tArr[]=$variant_options[$combi->product_variant_option_value_id];
}
When I var_dump $combination
, I get object(stdClass)#60 (8)//and the whole content
. So what's not okay with it?
I can also post the content of $combination
, but it's a lot of it.
Any idea what I should do?
Thanks
Edit: var_dump($combination) output:
array(8) { [0]=> object(stdClass)#60 (8) { ["id"]=> string(1) "1" ["product_variant_option_id"]=> string(1) "1" ["product_variant_option_value_id"]=> string(1) "1" ["sku"]=> string(0) "" ["price"]=> string(4) "0.00" ["saleprice"]=> string(4) "0.00" ["quantity"]=> string(1) "0" ["weight"]=> string(1) "0" }
[1]=> object(stdClass)#73 (8) { ["id"]=> string(1) "2" ["product_variant_option_id"]=> string(1) "1" ["product_variant_option_value_id"]=> string(1) "4" ["sku"]=> string(0) "" ["price"]=> string(4) "0.00" ["saleprice"]=> string(4) "0.00" ["quantity"]=> string(1) "0" ["weight"]=> string(1) "0" } }
Edit2:
foreach($variants as $variant){
$combination=$this->Product_model->getVariationCombination($variant->id);
$tArr=array();
foreach($combination as $combi){
$tArr[]=$variant_options[$combi->product_variant_option_value_id];
}
where getVariationCombination
is:
public function getVariationCombination($product_variant_id){
$query=$this->db->get_where('product_variant_combination',array('product_variant_option_id'=>$product_variant_id));
$variants=array();
foreach ($query->result() as $row)
{
$variants[]=$row;
}
if(count($variants)>0) return $variants;
return null;
}
to iterate over an object you have to implement an http://www.php.net/manual/en/class.iterator.php or http://www.php.net/manual/en/class.generator.php or http://www.php.net/manual/en/class.iteratoraggregate.php
I'm going to complete gries' reponse.
for you to use foreach you need to have an array to loop in. For that you need to do what gries said.
the reason why your var_dump
works is because an object is valid but you cannot loop through within the object's array of items like that. When you print_r
an object or var_dump
it may show you the properties as an array type looking output but it doesn't mean you can iterate it through it for a foreach