Jason Mayoff
Jason Mayoff

Reputation: 77

Accessing data within multidimensial array in PHP

This is what a var_dump looks like (below). I'm trying to access the "meta_key" and "meta_value" from each of the items in the array.

Assuming the name of the array is $the_array, I've tried things like:

$metakey = $the_array[0]["meta_key"];

and

$metakey = $the_array[0][2];

But nothing seems to be returned when I try those. In fact, there must be an error, because everything stops at that point. What am I doing wrong?

array(5) {
  [0]=>
  object(stdClass)#224 (4) {
    ["meta_id"]=>
    string(3) "184"
    ["post_id"]=>
    string(2) "56"
    ["meta_key"]=>
    string(17) "wpsr_product_link"
    ["meta_value"]=>
    string(63) "http://www.greenandblacks.com/ca/what-we-make/bars/dark-85.html"
  }
  [1]=>
  object(stdClass)#234 (4) {
    ["meta_id"]=>
    string(3) "182"
    ["post_id"]=>
    string(2) "56"
    ["meta_key"]=>
    string(17) "wpsr_product_name"
    ["meta_value"]=>
    string(34) "Green & Black's 85% Dark Chocolate"
  }
  [2]=>
  object(stdClass)#223 (4) {
    ["meta_id"]=>
    string(3) "183"
    ["post_id"]=>
    string(2) "56"
    ["meta_key"]=>
    string(18) "wpsr_product_price"
    ["meta_value"]=>
    string(5) "$3.49"
  }
  [3]=>
  object(stdClass)#236 (4) {
    ["meta_id"]=>
    string(3) "186"
    ["post_id"]=>
    string(2) "56"
    ["meta_key"]=>
    string(19) "wpsr_product_rating"
    ["meta_value"]=>
    string(3) "4.5"
  }
  [4]=>
  object(stdClass)#222 (4) {
    ["meta_id"]=>
    string(3) "185"
    ["post_id"]=>
    string(2) "56"
    ["meta_key"]=>
    string(20) "wpsr_product_summary"
    ["meta_value"]=>
    string(114) "Sed lobortis adipiscing turpis, tempus rutrum enim faucibus eget. Donec convallis arcu non massa convallis mollis."
  }
}

Upvotes: 0

Views: 58

Answers (1)

Tobias Golbs
Tobias Golbs

Reputation: 4616

You have objects inside your array so you access your property like that:

$metakey = $the_array[0]->meta_key;

Upvotes: 2

Related Questions