0

I have the following array but I am unsure how via a foreach I can break through the key so that I can use it in my HTML like this: <?php echo $key['cat_id'];?>

PHP:

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

Array:

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"
  }
Jess McKenzie
  • 8,345
  • 27
  • 100
  • 170

2 Answers2

2

If you are creating your array with json_decode, set the second parameter to true. I assume this is how that Object is being created?

$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;?> 

If not json, then cast it to an array: $by_category_manufacturer = (array)$by_category_manufacturer;

000
  • 26,951
  • 10
  • 71
  • 101
1

Convert PHP object to associative array

convert your object to associative array, this may be done recursively

Community
  • 1
  • 1
pinkdawn
  • 1,023
  • 11
  • 20