Jump to content

[SOLVED] PHP Implode Error


SJames

Recommended Posts

I have the following code, a simple implode function:

 

$titles = $_POST['titles'];
$titles = implode("#", $titles);

 

I am positive that "$titles" is an array, but I get an error message from PHP saying:

 

Warning: implode() [function.implode]: Bad arguments. in /home/.... on line 12

*I replaced the rest of the URL with "...." to save space.

 

Does anyone know why I'm getting this error? I've also tried removing the first value from the function, so it would read "implode($titles)". Using another variable with the implode doesn't work either, eg: "$newVariable = implode("#", $titles)"

Link to comment
https://forums.phpfreaks.com/topic/65302-solved-php-implode-error/
Share on other sites

if you receive "Array" and that's it when you run print_r() on the variable, it means you've got an empty array.  you may want to run a check to see if it's both an array and is NOT empty:

 

if (is_array($titles) && !empty($titles))
{
  $titles = implode('#', $titles);
}

 

EDIT:  it just dawned on me - if there are no parentheses after "Array", you probably actually just have a string with the value 'Array'.

if you're trying to run a foreach() on $titles AFTER using implode(), you're going to have problems because in the process of imploding (if it actually works), you've converted the array to a string due to the same variable name being used.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.