PHP内爆不适用于大数组?
I have an array with 30k items, and implode returns nothing. No error message, no memory problems, just nothing.
我有一个包含30k项的数组,内爆不会返回任何东西。没有错误消息,没有内存问题,什么都没有。
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.
如果我使用array_slice并将数组分割为100个条目,它就可以正常工作。它也适用于7k数组,但不适用于这个。
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 ','.
但是PHP_EOL不能在我的选择中使用,字符串需要被“,”分开。
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.
所以我有两个问题:有没有什么方法可以让这个工作,我怎样才能抓住这个错误?因为测试内爆输出不工作,is_null, strlen, is_string,空,isset,所有这些测试都失败了。
1 个解决方案
#1
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
编辑:在写完这个答案后的Facepalm时刻添加一个for循环?标记似乎并不比仅仅使用它来输出数据更好。不管怎样,我想你可以试试。
<?
$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 />";
}
?>
更多相关文章
- 学习成绩统计-【数组操作】
- 如何为$ wpdb-> insert创建一个包含column => value对的数组
- 为什么cron工作不是从浏览器工作,但从终端工作完美
- 如何在php中为json数组创建搜索功能
- json_encode在数组上返回null
- PHP数组值进入第二个数组[重复]
- 如何在php数组中插入新的键值对?
- mysql_fetch_array返回一个数组,数字为“1”
- PHP-从多维数组中删除重复值