0

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
)
3
  • So, you want us to do your work for you? Please show us what have you tried so far. Commented Sep 29, 2013 at 19:51
  • a foreach loop would bbe a good start, what have you tried? Commented Sep 29, 2013 at 19:51
  • he has enough repetitions to do him a little favor ;-) Commented Sep 29, 2013 at 20:12

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Also for the first statement ;-) $new_array[$a['lo']] = intval($a['co']);
0

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);

1 Comment

co is the count i think you should sum it up
0

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);

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.