2

I have the following code that is not returning as I expected. I was hoping the final 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 is returning data correctly and I can see the array it returns. To start, the $organizers array is a simple 1-D array of strings that all have small lengths under 20 chars. I think the issue might be that $organizers is more than 10,000 indices long. Are there limitations on the length of an array that can be imploded? Are there work-arounds for that? I cannot find anything in the manual, but I have tested this code thoroughly and I believe the error must be on implode().

BenM
  • 52,573
  • 26
  • 113
  • 168
usumoio
  • 3,500
  • 6
  • 31
  • 57
  • Have you tried with a similar, but smaller array? Are there any odd characters in the strings? – Jake N Dec 12 '12 at 18:09
  • 5
    Put some debugging output after your `implode()`. Are you simply running out of memory? Do you have error logging on? – Brad Dec 12 '12 at 18:10
  • 1
    @jakenoble, PHP treats strings like binary data. "Odd characters" have no bearing on PHP string functions. – Brad Dec 12 '12 at 18:10
  • 1
    I dont' know if there is a limitation, but what comes to my mind is taht you are also transforming an array into a string. This shouldn't be the problem in PHP, but try calling it a different variable for the result of implode? ie: $organizers_string = implode(', ', $organizers); Alternatively, post here a sample of the array result with print_r function – MAXIM Dec 12 '12 at 18:10
  • 2
    I had no issues running this: http://ideone.com/CYNcAy (it's an array with 15000 elements, each of them a string with exactly 23 characters) – NullUserException Dec 12 '12 at 18:13
  • 2
    I took NullUserException's test to a bigger extreme: 100,000 elements with each element being 32 characters, and [there was no limitation](http://viper-7.com/gBIogu). – nickb Dec 12 '12 at 18:15
  • 2
    @nick I love how `$entry = 'some small string under 20 chars'` when it's 32 characters long :) – NullUserException Dec 12 '12 at 18:17
  • 1
    Has `$organizers` ever been used in conjunction with an & reference at any point? This can cause unexpected results... – Pebbl Dec 12 '12 at 18:24
  • It does not seem that big.. but are you perhaps just running out of memory? What does your error log say? – Wrikken Dec 12 '12 at 18:43
  • Okay, I'll run some additional debugging. I set the script to use 256 megs if needed (before asking this question so I can rule some memory issues.) It is quite apparent that I am doing something wrong and have not merely hit a limitation. I will continue to search and update this post. Thank you all for the help. – usumoio Dec 12 '12 at 18:50
  • @MAXIM So it looks like you were right. My array is too big and reassignment as I have written it is not possible. I had to space it out as you suggested. If you add that as an answer I will gladly accept it for this question. – usumoio Dec 12 '12 at 19:16
  • @IamJohnGalt just did it. Thanks – MAXIM Dec 12 '12 at 19:27

3 Answers3

1

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 the equivalent of what .implode() should return when called on $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;

This seemed to work on writecodeline.com/php/

I've also experienced issues with older php builds when I've tried to explode/implode by a string with special characters in it and they were encapsulated by single quotes. I know it sounds crazy, but the double quotes might be necessary on some servers.

Reference: personal experience doing work on older production servers.

rnirnber
  • 607
  • 7
  • 17
  • Let us know if it works out. Maybe if you're still having problems with implode, you could see the edit I'm about to make above. – rnirnber Dec 12 '12 at 19:21
1

I dont' know if there is a limitation, but what comes to my mind is taht you are also transforming an array into a string. This shouldn't be the problem in PHP, but try calling it a different variable for the result of implode?

$organizers        = array_unique($organizers);     // this returns correctly
$organizers_string = implode(', ', $organizers);    // this returns nothing

// This gives it a different space 
MAXIM
  • 1,223
  • 1
  • 9
  • 16
0

I'd hate to think I'm stating the obvious, but doesn't implode only take a string as an argument? Maybe it should be something more 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);
rnirnber
  • 607
  • 7
  • 17