Possible limitation of the implode function in PHP

I have the following code that does not return as I expected. I was hoping the end result would be a string:

$organizers = array_unique($organizers); // this returns correctly $organizers = implode(', ', $organizers); // this returns nothing var_dump($organizers); // no data appears here exit; 

The array_unique() function returns the data correctly, and I can see the returned array. To begin with, the $organizers array is a simple one-dimensional array of strings, all of which have a short length of less than 20 characters. I think the problem may be that $organizers are over 10,000 indexes. Are there any restrictions on the length of the array that can be blown up? Is there any problem for this? I cannot find anything in the manual, but I have fully checked this code and believe that the error should be on implode() .

+2
source share
3 answers

I don't know if there is a limitation, but what comes to my mind is that you also transform the array into a string. This is not a problem in PHP, but try calling it another variable for the implode result?

 $organizers = array_unique($organizers); // this returns correctly $organizers_string = implode(', ', $organizers); // this returns nothing // This gives it a different space 
+1
source

Edit: And if for some reason implode () is still problematic.

 $organizers = array_unique($organizers); $neworganizers = ""; for($i = 0; $i < sizeof($organizers); $i++) { $neworganizers .= $organizers[$i]; if($i != sizeof($organizers) - 1) { $neworganizers .= ", "; } } 

// $ neworganizers is now equivalent to what .implode () should return when calling $ organizers

 $organizers = array(); $organizers[0] = "value1"; $organizers[1] = "value2"; $organizers[2] = "value3"; $organizers[3] = "value3"; $organizers = array_unique($organizers); // strips out last index $organizers = implode(', ', $organizers); // returns string of "value1, value2, value3" echo $organizers; 

It seemed to work on writecodeline.com/php/

I also had problems with old php assemblies when I tried to explode / explode with a string with special characters in it and they were encapsulated with single quotes. I know this sounds crazy, but double quotes may be required on some servers.

Link: personal experience on old server servers.

+1
source

I would not want to think that I am stating the obvious, but it will not explode, just taking the string as an argument? Maybe it should be something like this ...

 $organizers = array_unique($organizers); //I'm guessing what you wanted was an array of arrays? $neworganizers = array(); for($i = 0; $i < sizeof($organizers); $i++) { $neworganizers[$i] = implode(", ", $organizers); } print_r($neworganizers); 
0
source

All Articles