I have a multidimensional array in PHP, where the external array contains several thousand elements, and each element inside is the array itself with the values ββ"key1", "key2" and "count":
myExistingArray (size=99999 VERY BIG) public 0 => array (size=3) 'key1' => string '15504' 'key2' => string '20' 'count' => string '1' public 1 => array (size=3) 'key1' => string '15508' (length=5) 'key2' => string '20' (length=2) 'count' => string '2' (length=1) public 2 => array (size=3) 'key1' => string '15510' (length=5) 'key2' => string '20' (length=2) 'count' => string '5' (length=1) ....many more similar items
I want to convert this to a very simple array, where the old values ββfrom "key1" and "key" are combined as a new key that points to the "count" value for corresspond:
myNewArray (size=99999 VERY BIG) <key1>_<key2> => <count> 15504_20 => string '1' (length=1) 15508_20 => string '2' (length=1) 15510_20 => string '5' (length=1)
Performance is very important to me, since the external array has several thousand elements. Is there a quick method in PHP? The only thing I had was a simple iteration, but for me it gets in the way:
// works but I am looking for a faster version $myNewArray = array(); foreach ($myExistingArray as $item) { $myNewArray [$item["key1"]."_".$item["key1"]]=$item["count"]; }
EDIT / Main Problem
Some people rightfully added that my current solution is already in O (n) and it is mentioned that there is no built-in function in PHP to speed this up.
I get "myExistingArray" from the mysql database query. I basically have job objects and want to group them by their status and their event_id. The request is similar to this:
select count(job.id) as count, job.status as key1, job.event_id as key2 from job group by job.status, job.event_id
I want to change the order of the keys so that later I can easily access the job account for a specific event with a certain status.
arrays php multidimensional-array
Pascal klein
source share