0

I have an item object in PHP, which has the following structure on var_dump :

$item->properties:

array (size=1)
  1 => 
    array (size=4)
      'Key1' => string 'Value1' (length=6)
      'Key2' => int 1
      'Key3' => string 'true' (length=4)
      'Key4' => string 'true' (length=4)

I want to access Key, value in a foreach loop and assign Key, value pair to some internal variables, however when i am using the foloowing code to loop pver array of array, i am getting error in accessing the values in the way i want. Here is what i am doing :

        foreach($item->properties as $property) {
            foreach($property as $value) {
                echo $value;
            }
        }

Anyone have an idea what am i doing wrong, and how can i fix that ?

3
  • i am getting error in accessing the values -- what is the error? Commented Dec 16, 2013 at 19:09
  • " Warning: Invalid argument supplied for foreach() .... " Commented Dec 16, 2013 at 19:11
  • It simply means the argument is not an array. Why do you need the nested foreach anyway? foreach($item->properties as $property) { echo $key.' : '.$value; } -- does this not work? Commented Dec 16, 2013 at 19:21

1 Answer 1

1

one of the things you provide to the foreach isn't a a valid argument, as the error says. Find out which of the 2 it is (linenumber) and var_dump that argument to see what type it is (probably "not an array" ).

In the end either $item->properties itself, or the array values of that array (if it is one), so $property is not an array.

It could be, for instance, that maybe the first key of the properties IS an array, but the second isn't? then you could use is_array to check.

Sign up to request clarification or add additional context in comments.

Comments

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.