如何在循环中获得这个

时间:2017-03-15 12:34:14

标签: php magento

foreach ($item_variations as $key => $value) {
    foreach ($value as $k => $v) {
            if($k==$attributeCode)
            {
                if($v != ''){
                    Mage::log($v,NULL,"value.log");
                    $values[]=$v; 
                }

            }
         }
     }

这是我的数组$ item_variation

我想在循环中使用它,但因为它是对象数组,所以它会产生问题

我试过这个,但它只执行一次

并且离开了循环

{{1}}

因此建议在循环中运行此数组的解决方案

4 个答案:

答案 0 :(得分:1)

试试这个:

foreach ($item_variations as $key => $object) {
    $vars = get_object_vars($object);
    foreach ($vars as $k => $v) {
        if($k==$attributeCode) {
                if($v != ''){
                    Mage::log($v,NULL,"value.log");
                    $values[]=$v; 
                }
            }
        }
     }
}

答案 1 :(得分:0)

use $key => $object instead of $key => $value. because your array is object

foreach ($item_variations as $key => $object) {
    $vars = get_object_vars($object);
    foreach ($vars as $k => $v) {
        if($k==$attributeCode) {
                if($v != ''){
                    Mage::log($v,NULL,"value.log");
                    $values[]=$v; 
                }
            }
        }
     }
}

答案 2 :(得分:0)

尝试以下代码,它可以满足您的需求:

foreach ($item_variations as $item) {  
  foreach ($item as $key => $value) {
    if ($key == $attributeCode && !empty($value)) {
      Mage::log($value,NULL,"value.log");
      $values[]=$value;
    }
  }
}

答案 3 :(得分:0)

您好,如果您还没找到,请尝试此解决方案。根据您的数组数据,您应该具有此格式的数组。

$item_variations = array($stdClassobj1, $stdClassobj2);
    //to get loop going
     foreach($item_variations as $items){
       echo "item_variation_id:".$items->item_variation_id;
       echo "iteam Index:".$items->item_index;
       // you can do any further filter here using if else

     }

我没有测试过,但这是你应该循环的方式。