Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: implode() Invalid arguments passed [closed]

I am using Codeigniter and its validation rules - a custom callback validation. Anyway, this seems not to be CI related I think.

I've got this function to return a string …

function array_implode($a)
{
  return implode(',', $a);
}

… but I always get a message implode(): Invalid arguments passed

But var_dump() shows me this:

array(2) {
  [0]=> string(10) "First item"
  [1]=> string(11) "Second item"
}

What is wrong?

like image 802
suntrop Avatar asked Nov 30 '22 01:11

suntrop


1 Answers

Why? Why would you write a function, that calls a std function? Why not write implode(',', $array); instead of adding the overhead of a function call?

Also: What var_dump puts out an array? is it a dump of $a inside the array_implode function? To be sure $a is always going to be an array, and you insist on keeping your array_implode function, edit the code to look like this:

function array_implode(array $a)
{//type hinting: this function will only work if $a is an array
    return implode(',',$a);
}
like image 112
Elias Van Ootegem Avatar answered Dec 06 '22 07:12

Elias Van Ootegem