Jess McKenzie
Jess McKenzie

Reputation: 8385

Iterate an array for HTML

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"
  }

Upvotes: 0

Views: 69

Answers (2)

pinkdawn
pinkdawn

Reputation: 1033

Convert PHP object to associative array

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

Upvotes: 1

000
000

Reputation: 27227

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;

Upvotes: 2

Related Questions