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().

12
  • 5
    Put some debugging output after your implode(). Are you simply running out of memory? Do you have error logging on? Commented Dec 12, 2012 at 18:10
  • 1
    @jakenoble, PHP treats strings like binary data. "Odd characters" have no bearing on PHP string functions. Commented Dec 12, 2012 at 18:10
  • 2
    I had no issues running this: ideone.com/CYNcAy (it's an array with 15000 elements, each of them a string with exactly 23 characters) Commented Dec 12, 2012 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. Commented Dec 12, 2012 at 18:15
  • 2
    @nick I love how $entry = 'some small string under 20 chars' when it's 32 characters long :) Commented Dec 12, 2012 at 18:17

3 Answers 3

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 
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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);

1 Comment

Sorry! It's early and I got confused/mixed up with explode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.