0

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."
  }
}
1
  • Whenever something doesn't work, enable error_reporting. PHP provides help. Commented Jul 28, 2013 at 17:55

1 Answer 1

2

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

$metakey = $the_array[0]->meta_key;
Sign up to request clarification or add additional context in comments.

2 Comments

@JasonMayoff - If it worked, it would be good to accept his answer.
I tried to accept, but was told I had to wait 10 min. This was my first opportunity since then, to come back and click the check mark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.