Home > Net >  How to avoid warning array to string converstion in php when you passing param as string or array to
How to avoid warning array to string converstion in php when you passing param as string or array to

Time:07-10

I am having difficulty passing array as an argument. I am getting an error string to array conversion. the $optional variable sometimes need to get a string as a parameter and sometimes an array. Please check my code below. Many Thanks

  function getLists($str, array $items,$optional=null, ){

        $items1 = array_map(function($x) use ($optional) { return "$x $optional"; }, $items);
        $itemsCount = count($items1);
        $sentence = '';
        if ($itemsCount == 1) {
            $sentence = $items[0] . '.';
        } else {
            if($optional == null)
            {
                $partial = array_slice($items, 0, $itemsCount-1);
                $sentence = implode(', ', $partial) . ' and ' . $items[$itemsCount-1]; 

            }
            if(is_string($optional))
            {
                $partial = array_slice($items1, 0, $itemsCount-1);
                $sentence =   implode(', ', $partial). ' and ' . $items1[$itemsCount-1];
            }
        else
        {
            $partial = array_slice($items1, 0, $itemsCount-1);
            $sentence =   implode(', ', $partial). ' and ' . $items1[$itemsCount-1];
        }              
        }
                    
        return $str.': '.$sentence.'.';
    }

Here are what I am trying to do, the following two are working correctly

getList("What do you want",array("chocolate","icecream","shake"));
getList("Ice Cream has",array("chocolate","vanilla","mango"),"flavour");

But when I replace try to pass [] as parameter then I got an error array to string conversion warning

getList("The color required",array("green","red","blue"),['chilli','onion','berry']);

So when I pass this parameter and my output should be like: I am not getting the correct output that should be like:

The color required: green chilli,red onion and blue berry.

Instead I am getting:

The color required: green Array, red Array and blue Array.

CodePudding user response:

I suggest you to use the gettype function to test the type of $optional before applying any logic https://www.php.net/manual/fr/function.gettype.php

CodePudding user response:

In the first line of the body of your function you have a following statement:

return "$x $optional";

As you said, the $optional variable can be a string or an array. If it's a string there is no problem here. Your string will be inserted there. But if it's an array, then it's a syntax error. You're trying to put the whole array there. Instead you should put just one element of that array.

What you should do, is to replace the first line ($items1 = ...) of your function's body to:

$items1 = [];
if( is_array($optional) ) {
  for($i=0 ; $i < count($items) ; $i  ) {
    $items1[] = "$items[$i] $optional[$i]";
  }
} else {
  $items1 = array_map(function($x) use ($optional) { return "$x $optional"; }, $items);
}
  •  Tags:  
  • php
  • Related