John Davidson

How to avoid warning array to string converstion in php when you passing param as string or array to function?

0 comments
Message:


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.


My Answer:
To avoid the warning array to string conversion, you can modify your function to handle both string and array inputs for the $optional parameter. Here's an updated version of your function:

php
function getLists($str, array $items, $optional = null) {
$items1 = array_map(function($x) use ($optional) {
if (is_array($optional)) {
$optionalValue = array_shift($optional);
return "$x $optionalValue";
} else {
return "$x $optional";
}
}, $items);

$itemsCount = count($items1);
$sentence = '';

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

return $str . ': ' . $sentence . '.';
}


With this updated function, you can pass either a string or an array as the $optional parameter. If an array is passed, the function will use the first value from the array for each item in the $items array. Otherwise, it will use the single string value provided.

Now you can call the function like this:
php
getList("What do you want", array("chocolate", "icecream", "shake"));
getList("Ice Cream has", array("chocolate", "vanilla", "mango"), "flavour");
getList("The color required", array("green", "red", "blue"), ['chilli', 'onion', 'berry']);


This should give you the desired output without any warning about array to string conversion.

Rate this post

4 of 5 based on 9330 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us