PHP: implode() Invalid arguments passed [closed]
Asked Answered
T

3

5

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?

Twitt answered 13/5, 2013 at 8:28 Comment(3)
Try var_dump($a); I suspect that this is not an array :)Helm
can we see the code where you use it?Denison
where array_implode is called?This is not related to CI.It is due to datatypes in $aNeediness
V
8

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);
}
Vauntcourier answered 13/5, 2013 at 8:33 Comment(6)
In CodeIngiters form validation rules I can't pass additional parameters, thus I can't pass it my $glue string. Yes, I put the var_dump inside the array_implode funcition, right before the returnTwitt
@suntrop: CodeIgniter doesn't change the ZendEngine, but where am I adding $glue according to you? My suggestion is to use implode(',', $array) instead of array_implode($array); <-- replace function call by inline implode call. The only change I made to your code, in my snippet is added type hinting.Vauntcourier
your code edit shows the same message (Message: Argument 1 passed to Page::array_implode() must be of the type array, string given). But I don't get why it is a string? var_dump inside the same function says it is an array?!Twitt
@Suntrop: The error message says it all: the script halts because you're not passing an array to the function. My code (with type hinting) will error when you call the function, not inside the function, so you won't even see a var_dump. In short: you're calling the function several times, and one of those times, you're not passing an arrayVauntcourier
@suntrop: Also, because the error says Page::array_implode, this is a method, not a regular function... please make it private, protected or public explicitly... and add some code to your question. We're all guessing here, just show us what you're doingVauntcourier
Thanks. I think it is better to change my form an avoid this problem. I don't know why and where the problem comes from and I can't post all that code here. Any way, thanks for your help.Twitt
N
8

You can convert $a to array to make sure you are always working with arrays when using implode

function array_implode($a) {
    return implode(',', (array) $a);
}
Neophyte answered 13/5, 2013 at 8:50 Comment(1)
This is just error hiding: if $a is not array, no error is thrown and program continues with unexpected input - much worse than real error.Oralee
D
7

The code shouldn't throw any error. Probably there is something hidden. Use this function to find out the bug:

function array_implode($a)
{
  // print detailed info if $a is not array
  if(!is_array($a)) {
    var_dump($a); // what is in $a
    var_dump(debug_backtrace()); // where exactly was it called?
    exit;
  }
  return implode(',', $a);
}
Detailed answered 13/5, 2013 at 8:41 Comment(3)
About debug_bactrace: php.net/manual/en/function.debug-backtrace.phpPeradventure
… thanks. But this function will output a completre list of all CMS pages, configs and a lot moreTwitt
Then use the limit (see the link provided by CertaiN): debug_backtrace(true,4); will print just four level of nested callsOralee

© 2022 - 2024 — McMap. All rights reserved.