如何从数组的 foreach 循环创建数组?
How can I create an array from a foreach loop of arrays?
我有两个数组。第一个是通过制作数组块 $properties
.
创建的
$array1 = array_chunk($properties,10000,true);
看起来像这样:
array(4) {
[0]=>
array(10000) {
["12345"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "monkey.jpg"
["path"]=>
string(149) "Volumes/animals/monkey.jpg"
["size"]=>
string(7) "2650752"
}
["678790"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "elephant.jpg"
["path"]=>
string(171) "Volumes/animals/elephant.jpg"
["size"]=>
string(7) "2306688"
}
... and so on.
另一个是从数据库创建的:
$countResult = "SELECT COUNT(*) AS `count` FROM files";
$q = $pdo->prepare($countResult);
$q->execute();
$row = $q->fetch(PDO::FETCH_ASSOC);
$totalCount = $row['count'];
$chunkSize = 10000;
$from = 0;
while($from < $totalCount)
{
$sql = 'SELECT id, dateTime, fileName, path, size FROM files LIMIT ' . $from . ', ' . $chunkSize . ';';
$q = $pdo->prepare($sql);
$q->execute();
$rows = $q->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC);
echo "<pre>";
var_dump($rows);
echo "</pre>";
$from += $chunkSize;
}
var_dump($rows)
的结果是这样的:
array(10000) {
["12345"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "monkey.jpg"
["path"]=>
string(149) "Volumes/animals/monkey.jpg"
["size"]=>
string(7) "2650752"
}
["678790"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "elephant.jpg"
["path"]=>
string(171) "Volumes/animals/elephant.jpg"
["size"]=>
string(7) "2306688"
}
and so on...
array(10000) {
["23424"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:46:25"
["fileName"]=>
string(46) "rabbit.jpg"
["path"]=>
string(149) "Volumes/animals/rabbit.jpg"
["size"]=>
string(7) "2655752"
}
["163452"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 12:39:43"
["fileName"]=>
string(45) "snake.jpg"
["path"]=>
string(171) "Volumes/animals/snake.jpg"
["size"]=>
string(7) "2406688"
}
and so on...
所以 $array1
是一个拆分数组,不知何故看起来好像它仍然是一个包含 4 个数组的数组...(我不太明白这一点),但是:我需要创建来自 mysql 数据库输入的第二个数组 ($array2
) 看起来与 $array1
完全一样,我不知道如何实现。目的是我想比较 $array1
和 $array2
检查它们是否匹配。
在while之前创建数组,然后从数据库中推送数据:
//create the array here
$rows = [];
while($from < $totalCount)
{
$sql = 'SELECT id, dateTime, fileName, path, size FROM files LIMIT ' . $from . ', ' . $chunkSize . ';';
$q = $pdo->prepare($sql);
$q->execute();
//HERE $rows[]
$rows[] = $q->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC);
echo "<pre>";
var_dump($rows);
echo "</pre>";
$from += $chunkSize;
}
然后按照你想要的方式比较,数组$rows
和$array1
我有两个数组。第一个是通过制作数组块 $properties
.
$array1 = array_chunk($properties,10000,true);
看起来像这样:
array(4) {
[0]=>
array(10000) {
["12345"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "monkey.jpg"
["path"]=>
string(149) "Volumes/animals/monkey.jpg"
["size"]=>
string(7) "2650752"
}
["678790"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "elephant.jpg"
["path"]=>
string(171) "Volumes/animals/elephant.jpg"
["size"]=>
string(7) "2306688"
}
... and so on.
另一个是从数据库创建的:
$countResult = "SELECT COUNT(*) AS `count` FROM files";
$q = $pdo->prepare($countResult);
$q->execute();
$row = $q->fetch(PDO::FETCH_ASSOC);
$totalCount = $row['count'];
$chunkSize = 10000;
$from = 0;
while($from < $totalCount)
{
$sql = 'SELECT id, dateTime, fileName, path, size FROM files LIMIT ' . $from . ', ' . $chunkSize . ';';
$q = $pdo->prepare($sql);
$q->execute();
$rows = $q->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC);
echo "<pre>";
var_dump($rows);
echo "</pre>";
$from += $chunkSize;
}
var_dump($rows)
的结果是这样的:
array(10000) {
["12345"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "monkey.jpg"
["path"]=>
string(149) "Volumes/animals/monkey.jpg"
["size"]=>
string(7) "2650752"
}
["678790"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "elephant.jpg"
["path"]=>
string(171) "Volumes/animals/elephant.jpg"
["size"]=>
string(7) "2306688"
}
and so on...
array(10000) {
["23424"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:46:25"
["fileName"]=>
string(46) "rabbit.jpg"
["path"]=>
string(149) "Volumes/animals/rabbit.jpg"
["size"]=>
string(7) "2655752"
}
["163452"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 12:39:43"
["fileName"]=>
string(45) "snake.jpg"
["path"]=>
string(171) "Volumes/animals/snake.jpg"
["size"]=>
string(7) "2406688"
}
and so on...
所以 $array1
是一个拆分数组,不知何故看起来好像它仍然是一个包含 4 个数组的数组...(我不太明白这一点),但是:我需要创建来自 mysql 数据库输入的第二个数组 ($array2
) 看起来与 $array1
完全一样,我不知道如何实现。目的是我想比较 $array1
和 $array2
检查它们是否匹配。
在while之前创建数组,然后从数据库中推送数据:
//create the array here
$rows = [];
while($from < $totalCount)
{
$sql = 'SELECT id, dateTime, fileName, path, size FROM files LIMIT ' . $from . ', ' . $chunkSize . ';';
$q = $pdo->prepare($sql);
$q->execute();
//HERE $rows[]
$rows[] = $q->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC);
echo "<pre>";
var_dump($rows);
echo "</pre>";
$from += $chunkSize;
}
然后按照你想要的方式比较,数组$rows
和$array1