PHP implode not working for big array?
Asked Answered
P

1

6

I have an array with 30k items, and implode returns nothing. No error message, no memory problems, just nothing.

If I use array_slice and slice the array to 100 items, it works fine. It also works for 7k array, but not for this one.

However, in another topic I found this code, which works just fine:

$arr = array();
for ($i = 0; $i < 50000; $i++) {
$arr[] = str_shuffle('This sentance is of average length, which The Internet says is  aboout 14.2 words.'); 
}
echo implode(PHP_EOL, $arr);

But with PHP_EOL I can't use that in my select, the string needs to be seperated by ','.

So I have two questions: is there any way to make this work and how can I catch this error? Because testing the implode output does not work, is_null, strlen, is_string, empty, isset, all these tests fail.

Promptitude answered 25/2, 2014 at 13:12 Comment(21)
Is there anything in your error logs?Horsley
"i cant use that in my select". Why? What happens? Does it pop up with an error? Blank page? Does your browser shut down? Does your server start burning? Does the Apache service halt? Does it start sending spam? That said, you shouldn't be doing this. You're generating at least 3.91 MB ((50000 * 82) / 1024 / 1024) more content for every single request.Kyoko
has for example item nr. 7k1 an qoute in it's string? is everything urlencoded? addslashes() for example... or does it break the array!Dutra
try "<br>" instead of PHP_EOLTrautman
Is your array multidimensional? If it is implode wont workCognize
what about server timeout.. whats you're script running time!Dutra
Hello, i just test this on my server an looks working fine. So, do you enabled error reporting on your server ?Disallow
@Kyoko because this doesnt work: SELECT * FROM table WHERE (productID IN (30 31 32 33 etc);Promptitude
@user3351349 You're missing some commas?Kyoko
@TD_Nijboer The array is fine, its just ids from database, nothing wrong therePromptitude
@DaveChild I found this error in the log [24-Feb-2014 12:58:09] PHP Catchable fatal error: Object of class someClassName could not be converted to string in C:******************\someClassName.php on line 273Promptitude
@user3351349 What's on line 273 of someClassName.php?Kyoko
@Kyoko $p_ids = implode(',', $available_brands_array);Promptitude
@user3351349 One of your values in $available_brands_array isn't a string - it's a class of the type someClassName. Use print_r and figure out which, or use the following: pastebin.com/XJHg8dpHKyoko
@Cognize It is not multiarray. TD_Nijboer I dont think its a timeout problem, its not like that implode takes 2 minutes to return a blank pages, it takes like 1 sec.Promptitude
@Kyoko That script does not print anything, all values are string. It just simple IDs from db, [30] => 30 [31] => 31 [32] => 32 [33] => 33Promptitude
Why bother using implode. Pass the array in directly as a parametrized query. You should not be passing in your SQL parameters as text, not best practices. php.net/manual/en/pdo.prepare.phpBodine
Ok so maybe I spoke too soon. In doctrine 2 you can pass in an array.. I'm not sure PDO supports this. So I apologize for that. Was an idea to look at thoughBodine
Here is a thread for you to look at, there might be some solutions for you here: #920853Bodine
Here is another: #15867140 Basically if you make X question marks (using a loop or whatever) you can pass the array in as a parameter. Kind of ghetto. Probably better off just using a real database framework like Doctrine 2Bodine
Here you go bro: '('.implode(',',array_fill(0,sizeof($this->Values),'?')).')'; Dug that up in some old code that solves this exact problem. That fills err up w/ ? marksBodine
B
1

EDIT: Facepalm moment after writing this answer that adding a for loop to make ? marks doesn't seem any better than just using it to output the data. Anyway I suppose you could try

<?
 $questionMarks =implode(',',array_fill(0,sizeof($myarray),'?'));
?>

and see if that has more luck for you.

You can use parametrized queries to circumvent your issue.

<?php

$db = new PDO(...);

//$myarray is some random sized php array of potential myid values
$questionMarks='';

//check to see if runtime is acceptible for your applicaition
for ($i = 0; sizeof($myarray); $i++)
    $questionMarks=",?";
$questionMarks=substr($questionMarks,1,strlen($questionMarks)-1);

/* you could try implode(',',array_fill(0,sizeof($myarray),'?')) but as you said implode might not work */

$sql = 'select myfield from mytable where myid in ('.$questionMarks.')';
$sth = $db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute($myarray);
if (!$sth) {
    echo "\nPDO::errorInfo():\n";
    print_r($db->errorInfo());

}  


while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
   echo $row['myfield']  . "<br />";
}
?>
Bodine answered 25/2, 2014 at 16:9 Comment(1)
Also check for database errors.. you might be creating invalid SQL via some data in your array that is not correct.Bodine

© 2022 - 2024 — McMap. All rights reserved.