【问题标题】:How can I create an array from a foreach loop of arrays?如何从数组的 foreach 循环创建数组?
【发布时间】:2018-05-16 17:28:58
【问题描述】:

我有两个数组。第一个是通过制作数组 $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 个数组的数组...(我不太明白这一点),但是:我需要创建第二个数组( $array2) 来自 mysql 数据库输入,看起来与 $array1 完全一样,我不知道如何实现。目的是我想比较$array1$array2 检查它们是否匹配。

【问题讨论】:

  • 数组只是一个容器。所以它可以包含数组。 (多维数组)。将第二部分放入数组中很容易,只需将 $super_rows[] = $rows 放入您的外观中并 var 转储 $super_rows 但比较它们很有趣

标签: php mysql arrays foreach chunks


【解决方案1】:

在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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-20
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2013-11-22
    相关资源
    最近更新 更多