0

Hey, so I have this array, called $files (var_dump()ed):

array(2) {
  [0]=>
  array(1) {
    ["_c"]=>
    array(2) {
      ["title"]=>
      array(1) {
        ["_v"]=>
        string(9) "blueprint"
      }
      ["src"]=>
      array(1) {
        ["_v"]=>
        string(20) "blueprint/screen.css"
      }
    }
  }
  [1]=>
  array(1) {
    ["_c"]=>
    array(2) {
      ["title"]=>
      array(1) {
        ["_v"]=>
        string(7) "general"
      }
      ["src"]=>
      array(1) {
        ["_v"]=>
        string(11) "general.css"
      }
    }
  }
}

but when I:

foreach($files as $file){
    // etc.
}

I get an Invalid argument supplied for foreach()

Any ideas? thanks.

6
  • 7
    It is not possible. Give the complete code that reproduces this issue. Commented May 9, 2011 at 1:49
  • My guess a non array/object has clobbered $files. Commented May 9, 2011 at 1:53
  • @zerkms pastebin.com/9w1XUu0P Commented May 9, 2011 at 1:58
  • @nomaD: it is not complete code. Add all variables definitions to that snippet so we can just copy-paste it, run and see the issue. Commented May 9, 2011 at 1:59
  • pastebin.com/rCp6EbPn line 114 is the one that gives an error Commented May 9, 2011 at 2:12

1 Answer 1

2

Ahhh. I see what you're doing. You are quite correct that for the css key you get file tags and thus a valid array:

    <css>
        <file>
            <title>blueprint</title>
            <src>blueprint/screen.css</src>
        </file>
        <file>
            <title>general</title>
            <src>general.css</src>
        </file>
    </css>

But how about when the js key comes up?

    <js>

    </js>

Sure enough, there is no file key, there is no array, and PHP chokes.

You may want to throw a

if (!isset($files['file'])) continue;

in there.

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

4 Comments

The key 'file' is actually brought in from the xml file. On line 105 you can see I go through it as an array, just not in a while() loop. var_dump()ing $files after line 113 (aka $files['file']) gives me the array in the original question. See: pastebin.com/qvGbX4K2
I misread that. Edited with a completely different (and must more tangible) explanation.
Ok now this is REALLY doing my head in, I just added in another while loop before the one that breaks: pastebin.com/nhJrJCT6 They both work fine, but also give me warnings at the same time!
Yeah it turns out the error was from the js array (which now that I think about it wouldn't exist because there were no files in the js area). Problem solved 100% thanks a lot mate! :D

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.