Is there a clean way to arrange this array so that the key becomes id? I have done this several times using a couple foreach and a new array where I put the info, but I am using around 7 lines of code and was wondering if there is anything cleaner. The array in question is:
Array (
[0] => Array ( [log_id] => 6 [type] => Test [other_info] => MoreInfo )
[1] => Array ( [log_id] => 5 [type] => Test2 [other_info] => MoreInfo2 )
)
So what I want to obtain from the above array is:
Array (
[6] => Array ( [type] => Test [other_info] => MoreInfo )
[5] => Array ( [type] => Test2 [other_info] => MoreInfo2 )
)
You can see that I put log id as the key now. This is taking me several lines of code... Do you have an idea of how this can be achieved in 3 or 4 lines at most?
Alternatively, is there a cleaner way to access the row containing the *log_id* that I want? The use that I want to give to this is that I can access the row of a certain *log_id*... In this case I would do it with $array[$log_id]
... But if there is a solution to do this without altering the array (and with only 1 line) I will accept that as an answer too!
Thanks!
But why do you need multiple foreach statements? Wouldn't this answer your question:
$new_arr = array();
foreach ($old_arr as $row) {
$new_arr[$row['log_id']][] = $row;
}
The only difference is that the new rows will still have the log_id key, but I doubt that's going to bother you. Except that, I find it very clear.