迭代一个HTML数组

时间:2013-04-09 02:45:36

标签: php arrays iteration

我有以下数组,但我不确定如何通过foreach突破key,以便我可以在我的HTML中使用它:<?php echo $key['cat_id'];?>

PHP:

<?php foreach($by_category_manufacturer as $key ):?>
      <pre><?php var_dump($key)?></pre>
  <?php endforeach;?> 

阵列:

array(6) {
  [0]=>
  object(stdClass)#92 (5) {
    ["brand_name"]=>
    string(5) "Kioti"
    ["brand_id"]=>
    string(2) "10"
    ["image_id"]=>
    string(2) "23"
    ["cat_id"]=>
    string(1) "3"
    ["cat_name"]=>
    string(9) "Machinery"
  }

2 个答案:

答案 0 :(得分:2)

如果使用json_decode创建数组,请将第二个参数设置为true。我假设这是创建对象的方式吗?

$by_category_manufacturer = json_decode($json_string, true);

<?php foreach($by_category_manufacturer as $key => $object ):?>
    <pre><?php var_dump($object['cat_id'])?></pre>
<?php endforeach;?> 

如果不是json,则将其转换为数组:$by_category_manufacturer = (array)$by_category_manufacturer;

答案 1 :(得分:1)

Convert PHP object to associative array

将您的对象转换为关联数组,这可以递归地完成