0

I haven't been able to find a solution in the suggested questions that would help me solve this issue.

I have the following code, which does work, but it throws a PHP Warning: Invalid argument supplied for foreach() error.

In my code, I am adding any authors from $author2 to the $authdisplay array that have the same role as the primary author ($auth_role[0]).

I have checked to make sure that the $author2, $auth2_role, $auth_role, and $authdisplay are all arrays, and $author2 and $auth2_role have the same number of elements. ($author is a string.)

Any tips and help figuring this out would be appreciated.

   $author = $this->getPrimaryAuthor();
   $author2 = $this->getAuthor2Names();
   $auth_role = $this->getPrimaryAuthorsRoles();
   $auth2_role = $this->getAuthor2Roles();
   $authdisplay[] = $author;

  foreach($author2 as $key=>$field) {
       if ($auth2_role[$key] == $auth_role[0]) {
          $authdisplay[] = $field;
       }
   }
   return $authdisplay;
 }
  • Have you tried to do `var_dump($author2)`? – sevavietl May 18 '16 at 21:13
  • You can also just merge two arrays `$author` and `$author2` with `array_merge($author, $author2)`, but applying `array_filter()` to `$author2` before this. – sevavietl May 18 '16 at 21:16
  • @sevavietl But `$author` isn't an array. – Barmar May 18 '16 at 21:17
  • most probably your `$author2` is `null` or `string` but should be `array` – luchaninov May 18 '16 at 21:18
  • 1
    I know you say you checked to make sure that `$author2` is an array, but you must have made a mistake. The only reason for this error is that `$author2` is not an array. – Barmar May 18 '16 at 21:18
  • Yeah, my bad. Anyway, I think there is no need to use `foreach()` when you can simply filter `$author2` array and then add `$author` to the begging. – sevavietl May 18 '16 at 21:20
  • var_dump($author2) returns an array of strings like this: array(34) { [0]=> string(7) "STRING1"... [33]=> string(9) "STRING33"} I also used echo is_array on each of the variables and echo gettype on each of the variables. Is there another way to check what the underlying issue may be? –  May 18 '16 at 21:22
  • Possible duplicate of [Invalid argument supplied for foreach()](http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – miken32 May 18 '16 at 21:24
  • The class method getAuthor2Names() isn't returning an iterable array/object. – Kevin_Kinsey May 18 '16 at 21:25
  • Author2Names() returns an array of strings. I can echo each string and key value out to the display just fine. –  May 18 '16 at 21:29
  • Are you sure that this exact `foreach()` causing the trouble? Maybe there is another one somewhere else? – sevavietl May 18 '16 at 21:29
  • Good point. I checked by commenting out this foreach(), and no error was thrown. So I think it's safe to say that it is this foreach(). The odd thing is that I use almost the exact same call in a different method and it works fine. –  May 18 '16 at 21:39

1 Answers1

0

Does it matter if $author2 is empty?

Typically you do this:

if (is_array($author2) && !empty($author2)) {
    foreach($author2 as $key=>$field) {
    #.... blah blah

    if (is_array($authdisplay) && !empty($authdisplay)) {
        return $authdisplay;
    }
    return false;
}
Kevin_Kinsey
  • 2,285
  • 1
  • 22
  • 23