PHP is the fastest way to convert a 2d array to a 3d array, which is grouped by a specific value

I would like to convert this two-dimensional array of entries:

[records] => Array
(
  [0] => Array
  (
    [0] => Pears
    [1] => Green
    [2] => Box
    [3] => 20
  )
  [1] => Array
  (
    [0] => Pears
    [1] => Yellow
    [2] => Packet
    [3] => 4
  )
  [2] => Array
  (
    [0] => Peaches
    [1] => Orange
    [2] => Packet
    [3] => 4
  )
  [3] => Array
  (
    [0] => Apples
    [1] => Red
    [2] => Box
    [3] => 20
  )
)

In this three-dimensional array, where each key of the array is grouped by a specific value from the original array:

[converted_records] => Array
(
  [Pears] => Array
  (
    [0] => Array
    (
      [0] => Green
      [1] => Box
      [2] => 20
    )
    [1] => Array
    (
      [0] => Yellow
      [1] => Packet
      [2] => 4
    )
  )
  [Peaches] => Array
  (
    [0] => Array
    (
      [0] => Orange
      [1] => Packet
      [2] => 4
    )
  )
  [Apples] => Array
  (
    [0] => Array
    (
      [0] => Red
      [1] => Box
      [2] => 20
    )
  )
)

I can do it like this:

$array = // Sample data like the first array above
$storage = array();
$cnt = 0;
foreach ($array as $key=>$values) {
  $storage[$values[0]][$cnt] = array (
    0 => $values[1],
    1 => $values[2],
    2 => $values[3]
  );
  $cnt ++;
}

I wanted to know if there is a better way to do this. I don't know about any functions in PHP that are capable of this, so I can only assume that this is basically how it will be done.

The problem is that it repeats so many times and every little millisecond will count, so I really want to know what is the best way to accomplish this task?

EDIT

An array of records is created by parsing the .CSV file as follows:

$records = array_map('str_getcsv', file('file.csv'));

EDIT No. 2

10 ( 5 . ), 0,645478 . , , , .

โ„– 3

20- . 14.91971.

- @num8er $records[$key][] = array_shift($data);, , .

, , .

, $records[$key][] = $data;, 18.03699 gc_collect_cycles(), .

, @num8ers , .

+2
2

() (1- )
array_map ( )
foreach (3- )
, .

3 . 100K ? 300 . ?
- . 1 - (100K == 100K ):

ini_set('memory_limit', '1024M');
set_time_limit(0);

$file = 'file.csv';
$file = fopen($file, 'r');

$records = array();
while($data = fgetcsv($file)) {
  $key = $data[0];
  if(!isset($records[$key])) {
    $records[$key] = array();
  }

  $records[$key][] = array(0 => $data[1],
                           1 => $data[2],
                           2 => $data[3]);
  gc_collect_cycles();
}

fclose($file);


โ†’ :

<?php

ini_set('memory_limit', '1024M');
set_time_limit(0);

function child_main($file)
{
    $my_pid = getmypid();
    print "Starting child pid: $my_pid\n";

    /**
     * OUR ROUTINE
     */

    $file = fopen($file, 'r');
    $records = array();
    while($data = fgetcsv($file)) {
        $key = $data[0];
        if(!isset($records[$key])) {
            $records[$key] = array();
        }

        $records[$key][] = array(0 => $data[1],
            1 => $data[2],
            2 => $data[3]);
        gc_collect_cycles();
    }
    fclose($file);

    unlink($file);

    return 1;
}


$file = __DIR__."/file.csv";
$files = glob(__DIR__.'/part_*');
if(sizeof($files)==0) {
    exec('split -l 1000 '.$file.' part_'); 
    $files = glob(__DIR__.'/part_*');
}

$children = array();
foreach($files AS $file) {
    if(($pid = pcntl_fork()) == 0) {
        exit(child_main($file));
    }
    else {
        $children[] = $pid;
    }
}

foreach($children as $pid) {
    $pid = pcntl_wait($status);
    if(pcntl_wifexited($status)) {
        $code = pcntl_wexitstatus($status);
        print "pid $pid returned exit code: $code\n";
    }
    else {
        print "$pid was unnaturally terminated\n";
    }
}

?>
+1

:

$array   = array_map('str_getcsv', file('file.csv'));

$storage = array();
foreach ($array as $values) {
    $key             = array_shift($values);
    $storage[$key][] = $values;
}

, .

+2

All Articles