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?

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
suntrop
  • 775
  • 3
  • 10
  • 24

3 Answers3

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);
}
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • 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 return – suntrop May 13 '13 at 08:42
  • 1
    @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_. – Elias Van Ootegem May 13 '13 at 08:45
  • 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?! – suntrop May 13 '13 at 08:51
  • @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 array – Elias Van Ootegem May 13 '13 at 08:53
  • @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 doing – Elias Van Ootegem May 13 '13 at 08:56
  • 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. – suntrop May 13 '13 at 09:03
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);
}
Baba
  • 94,024
  • 28
  • 166
  • 217
  • 2
    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. – Jan Turoň May 13 '13 at 08:53
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);
}
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • 1
    About `debug_bactrace`: http://www.php.net/manual/en/function.debug-backtrace.php – mpyw May 13 '13 at 08:44
  • … thanks. But this function will output a completre list of all CMS pages, configs and a lot more – suntrop May 13 '13 at 08:47
  • Then use the limit (see the link provided by CertaiN): `debug_backtrace(true,4);` will print just four level of nested calls – Jan Turoň May 13 '13 at 08:51