I would like to convert this two dimensional array of records:
[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
)
)
Into this three dimensional array where each array key is grouped by a certain 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 this like so:
$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 more optimal way to do this. I am not aware of any functions within PHP that are capable of this so I can only assume that this is basically how it would be done.
The problem is though, this is going to be repeated so so many times and every little millisecond is going to count so I really want to know what is the best way to accomplish this task?
EDIT
The records array is created by parsing a .CSV file as follows:
$records = array_map('str_getcsv', file('file.csv'));
EDIT #2
I did a simple benchmark test on a set of 10 results (5k records each) to get an average runtime of 0.645478 seconds. Granted there is a few other things going on before this so this is not a true indication of actual performance but a good indication for comparison to other methods.
EDIT #3
I did a test with about 20x the records. The average of my routine was 14.91971.
At some point the answer below by @num8er had $records[$key][] = array_shift($data);
before updating the answer as it is now.
When I tried testing with the larger set of results this it ran out of memory as its generating an error for each record.
This being said, once i did $records[$key][] = $data;
the routine completed with an average of 18.03699 seconds with gc_collect_cycles()
commented out.
I've reached the conclusion that although @num8ers method is faster for smaller files, for larger ones my method works out quicker.