Jeffz
Jeffz

Reputation: 2105

grouping array elements according to other array elements

start with something like this:

array (
  0 => 
  array (
    'co' => '1',
    'lo' => 'aaa',
  ),
  1 => 
  array (
    'co' => '1',
    'lo' => 'bbb',
  ),
  2 => 
  array (
    'co' => '1',
    'lo' => 'ccc',
  ),
  3 => 
  array (
    'co' => '1',
    'lo' => 'aaa',
  ),
  4 => 
  array (
    'co' => '1',
    'lo' => 'bbb',
  )
)

Then group array elements with index 'lo', counting values of 'co' which accompany them, to get eventually something similar to:


array (
  aaa => 2,
  bbb => 2,
  ccc => 1
)

Upvotes: 0

Views: 79

Answers (3)

shennan
shennan

Reputation: 11666

I agree with the others about showing the code that you've tried so far. Having said that, this should work:

$array = array (
  array (
    'co' => '1',
    'lo' => 'aaa',
  ),
  array (
    'co' => '1',
    'lo' => 'bbb',
  ),
  array (
    'co' => '1',
    'lo' => 'ccc',
  ),
  array (
    'co' => '1',
    'lo' => 'aaa',
  ),
  array (
    'co' => '1',
    'lo' => 'bbb',
  )
);

$new_array = array();

foreach($array as $a){
  if(!array_key_exists($a['lo'], $new_array)){
    $new_array[$a['lo']] = intval($a['co']);
  }else{
    $new_array[$a['lo']] = $new_array[$a['lo']] + intval($a['co']);
  }
}

print_r($new_array);

We're cycling through the array, creating a new array key if one doesn't exist, and adding to the array key value with the number specified in 'co' if it already exists. You may want some additional checks to be sure that 'co' and 'lo' exist as array keys in the original array before trying to parse/add them to your new array.

As a side note, there is no need to specify the keys of your original array as numbers because arrays are automatically indexed. Note that I have removed these numbers when declaring the array.

Upvotes: 1

pknoe3lh
pknoe3lh

Reputation: 374

Here you are ;-)

$in=array (
  0 => 
  array (
    'co' => '1',
    'lo' => 'aaa',
  ),
  1 => 
  array (
    'co' => '1',
    'lo' => 'bbb',
  ),
  2 => 
  array (
    'co' => '1',
    'lo' => 'ccc',
  ),
  3 => 
  array (
    'co' => '1',
    'lo' => 'aaa',
  ),
  4 => 
  array (
    'co' => '1',
    'lo' => 'bbb',
  )
);

$out= array();

for($i=0;$i<count($in);$i++)
{
    $out[$in[$i]['lo']]=0;
}

for($i=0;$i<count($in);$i++)
{
    $out[$in[$i]['lo']]+=$in[$i]['co'];
}

print_r($out);

Upvotes: 0

skmail
skmail

Reputation: 423

Like this ?

$dataArray = array (
    0 =>
    array (
        'co' => '1',
        'lo' => 'aaa',
    ),
    1 =>
    array (
        'co' => '1',
        'lo' => 'bbb',
    ),
    2 =>
    array (
        'co' => '1',
        'lo' => 'ccc',
    ),
    3 =>
    array (
        'co' => '1',
        'lo' => 'aaa',
    ),
    4 =>
    array (
        'co' => '1',
        'lo' => 'bbb',
    )
);

$finalResults = array();

foreach($dataArray as $data){
    if(isset($finalResults[$data['lo']])){
        $finalResults[$data['lo']]++;
    }else{
        $finalResults[$data['lo']] = 1;
    }
}


print_r($finalResults);

Upvotes: 0

Related Questions