1

I print a PHP array with var_export and I get something that looks like this :

array (
  0 => 
  array (
    'id' => 1,
    'type' => 'POSTER',
    'brand' => NULL,
    'model' => 'Enhanced Matte Paper Poster (in)',
    'image' => 'https://d1yg28hrivmbqm.cloudfront.net/products/1/product_1552909388.jpg',
    'variant_count' => 11,
    'currency' => 'USD',

I try to access elements of array using this :

  //Get product list
        $products = $pf->get('products');
       highlight_string(var_export($products, TRUE));
        
       foreach($products as $product){
        echo $product[0]->id
}

I get 2 errors

Notice: Undefined offset: 0

Notice: Trying to get property of non-object

I did read this here but I think I'm doing something wrong. What I am missing?

Community
  • 1
  • 1

2 Answers2

1

You don't have objects here, just array of arrays:

foreach($products as $product){
    echo $product['id'];
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64
1

It's a Array dude, do you access like

$products = $pf->get('products');
highlight_string(var_export($products, TRUE));

foreach($products as $product){
    echo $product['id'];
}