Suppose I have an array such as the one below. If I wanted to separate and divide out the array below into multiple arrays containing records with only one tl_id, how would I go about doing this efficiently?
Example: so notice how there are two tl_id’s (there could possibly be more): 275 and 328. I want two separate out that big array into two arrays. array1 would contain all the records that has tl_id’s of 275. And array2 would contain all the records with tl_id 328.
I was thinking of a for loop and separating it out that way, but was hoping there’s some PHP wizardy function I can use to simplify this action.
array(5) {
[0] => array(14) {
["tl_id"] => int(275)
["tl_email"] => string(27) "abc@gmail.com"
["tl_first_name"] => string(9) "Jane"
["tl_last_name"] => string(6) "Doe"
["target_id"] => int(354)
["target_first_name"] => string(4) "Rudy"
["target_last_name"] => string(5) "Smith"
}
[1] => array(14) {
["tl_id"] => int(275)
["tl_email"] => string(27) "abc@gmail.com"
["tl_first_name"] => string(9) "Jane"
["tl_last_name"] => string(6) "Doe"
["target_id"] => int(354)
["target_first_name"] => string(4) "Rudy"
["target_last_name"] => string(5) "Smith"
}
[2] => array(14) {
["tl_id"] => int(275)
["tl_email"] => string(27) "abc@gmail.com"
["tl_first_name"] => string(9) "Jane"
["tl_last_name"] => string(6) "Doe"
["target_id"] => int(194)
["target_first_name"] => string(5) "Katie"
["target_last_name"] => string(4) "Smith"
}
[3] => array(14) {
["tl_id"] => int(328)
["tl_email"] => string(20) "qrf@hotmail.com"
["tl_first_name"] => string(6) "John"
["tl_last_name"] => string(9) "Smith"
["target_id"] => int(219)
["target_first_name"] => string(6) "Kelly"
["target_last_name"] => string(5) "Smith"
}
[4] => array(14) {
["tl_id"] => int(328)
["tl_email"] => string(20) "qrf@hotmail.com"
["tl_first_name"] => string(6) "John"
["tl_last_name"] => string(9) "Smith"
["target_id"] => int(213)
["target_first_name"] => string(5) "Chris"
["target_last_name"] => string(5) "Jones"
}
}
I hope this will be of help.
This function walks the array and builds a new array where the items have been grouped by id.