简体   繁体   中英

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
)

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM