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 =
$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 , .