0

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 am 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!

4
  • Please format your code samples for easier legibility. Commented Aug 9, 2011 at 16:30
  • What code do you already have? Commented Aug 9, 2011 at 16:31
  • Whatever you are doing looks incorrect. Rather than answering this, can you explain what you are trying to do? Commented Aug 9, 2011 at 16:32
  • Got that from a mysql query, and since the other table is in a non-federated table on another server, I need to get some values by hand; in this case, I am in need of grouping by site_id and keeping the relationship between link_id and count(*) in order to do operations with them. Commented Aug 9, 2011 at 16:45

1 Answer 1

3

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);
} ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.