Rick de Vries
Rick de Vries

Reputation: 13

Trying to split array. Working but showing error

I want to split an array in php with explode. My array looks likes this:

C:\wamp64\www\SC New Skin\functions\get\userItems.php:21:
array (size=1)
  0 => 
    array (size=6)
      0 => 
        array (size=0)
          empty
      1 => string '1,1' (length=3)
      2 => string '2,11' (length=4)
      4 => string '1,8' (length=3)
      5 => string '2,10' (length=4)
      8 => string '1,6' (length=3)

I want to split on the ",".

My code looks like this:

function getItems($array){
    $array = array_unique($array);
    foreach ($array as $key) {
        $var = explode(",", $key);
        echo $var[0];
        echo $var[1] . '<br/>';
    }
}

This will work but gives two errors:

Notice: Array to string conversion in C:\wamp64\www\SC New Skin\functions\get\userItems.php on line 20 & Warning: explode() expects parameter 2 to be string, array given in C:\wamp64\www\SC New Skin\functions\get\userItems.php on line 22

Upvotes: 1

Views: 55

Answers (1)

BlakSneil
BlakSneil

Reputation: 206

This may be because the first element of the internal array is also an array, not a string like the other elements:

0 => 
   array (size=0)
        empty

Upvotes: 2

Related Questions