possible limitation of implode function in PHP
Asked Answered
S

3

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

Scutch answered 12/12, 2012 at 18:6 Comment(12)
Have you tried with a similar, but smaller array? Are there any odd characters in the strings?Merriott
Put some debugging output after your implode(). Are you simply running out of memory? Do you have error logging on?Undermine
@jakenoble, PHP treats strings like binary data. "Odd characters" have no bearing on PHP string functions.Undermine
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 functionPare
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)Martica
I took NullUserException's test to a bigger extreme: 100,000 elements with each element being 32 characters, and there was no limitation.Barbie
@nick I love how $entry = 'some small string under 20 chars' when it's 32 characters long :)Martica
Has $organizers ever been used in conjunction with an & reference at any point? This can cause unexpected results...Krieg
It does not seem that big.. but are you perhaps just running out of memory? What does your error log say?Anatola
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.Scutch
@Pare 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.Scutch
@IamJohnGalt just did it. ThanksPare
P
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 
Pare answered 12/12, 2012 at 19:27 Comment(0)
S
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.

Sandasandakan answered 12/12, 2012 at 18:52 Comment(1)
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.Sandasandakan
P
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 
Pare answered 12/12, 2012 at 19:27 Comment(0)
S
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);
Sandasandakan answered 12/12, 2012 at 18:32 Comment(1)
Sorry! It's early and I got confused/mixed up with explode.Sandasandakan

© 2022 - 2024 — McMap. All rights reserved.