PHP: A clean way of converting an array
What is a clean way to convert an array that looks like this:
[584] => Array ( [link_id] => 1 [site_id] => 5 [COUNT(*)] => 2 )
[585] => Array ( [link_id] => 243 [site_id] => 5 [COUNT(*)] => 2 )
[586] => Array ( [link_id] => 522 [site_id] => 89223 [COUNT(*)] => 3 )
To an array where the key is the site_id from above, so for the above example the resulting array would be:
[5] => Array( 1, 2, 243, 2) //Even ones and 0 are link_id, odd ones are count(*)
[89223] => Array(522, 3)
So, basically, group them by site_id. I still need to keep the relationship between link_id and count(*), in the case above I a开发者_如何学Pythonm doing it by the positions (so 0 and 1 are together, 2 and 3, etc) but I am open to a new structure as well.
Thanks!
You mean something like this? (Demo):
$out = array();
foreach($input as $v)
{
$site_id = $v['site_id'];
unset($v['site_id']);
$out[$site_id][] = $v;
}
Or in case you prefer value pairs after each other (Demo):
...
unset($v['site_id']);
$out[$site_id][] = array_shift($v);
$out[$site_id][] = array_shift($v);
} ...
精彩评论