Better method for flipping multidimensional array?
- by sudowned
I've retrieved some data from the database, which is in the following structure:
[0]
[item_id] = 197
[dice_chat_css] = "foo"
[dice_image] = "bar.png"
[1]
[item_id] = 128
[dice_chat_css] = "foo"
[dice_image] = "bar.png"
The most convenient and computationally inexpensive way for me to pass this data to the rest of my (PHP) application is with item_id as the index, because it saves having to loop over the array to look up values. If this was a flat array, I could accomplish this trivially with array_flip, but since it isn't, it's my pick of using either the multidimensional array_flip listed in the comments on PHP.net, or roll my own logic:
for ($i = 0; $i < sizeOf($r); $i++){
$s[$r[$i]['item_id']]['dice_image'] = $r[$i]['dice_image'];
$s[$r[$i]['item_id']]['dice_chat_css'] = $r[$i]['dice_chat_css'];
}
I know it's simple, but it feels like I'm reinventing the wheel here. Is there an accepted, more optimized method available or am I being weird about this?