peace_love
peace_love

Reputation: 6471

How can I create an array from a foreach loop of arrays?

I have two arrays. The first one is created by making chunks of the array $properties.

$array1 = array_chunk($properties,10000,true);

It looks like this:

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.

The other one is created from the database:

  $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;
}

The result of var_dump($rows) is this:

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

So $array1 is a splitted array, that somehow looks like as if it is still one array that contains 4 arrays... (I do not really understand this), but however: I need to create a second array ($array2) from the mysql database input that will look exactly like $array1 and I do not know how to achieve is. The purpose is that I want to compare $array1 and $array2 the check if they are matching.

Upvotes: 0

Views: 52

Answers (1)

Felippe Duarte
Felippe Duarte

Reputation: 15131

Create the array before the while, then push the data from database:

//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;
}

Then compare the way you want, the array $rows with $array1

Upvotes: 1

Related Questions