Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to merge and calc only by choosen key. For example I have the following array:

$array = array();
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 2];
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 1];

$array['protein'][] = ['id' => 16, 'name' => 'soy yogurt', 'type' => 'protein', 'measurement' => '', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 1];

$array['lipid'][] = ['id' => 19, 'name' => 'coconut powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 20, 'name' => 'chocolate powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 38, 'name' => 'chocolate square', 'type' => 'lipid', 'measurement' => '', 'amount' => 1];

Final output should look like this :

Array ( 
    [0] => Array ( 
        [id] => 17 
        [name] => seasonal fruit 
        [type] => Fruit 
        [measurement] => 
        [amount] => 3
    ) 
    [1] => Array ( 
        [id] => 16 
        [name] => soy yogurt 
        [type] => protein 
        [measurement] => 
        [amount] => 2 
    ) 
    [2] => Array ( 
        [id] => 18 
        [name] => oilseed 
        [type] => protein 
        [measurement] => grip 
        [amount] => 3
    ) 
    [3] => Array ( 
        [id] => 19 
        [name] => coconut powder 
        [type] => lipid 
        [measurement] => Tablespoon 
        [amount] => 2 
    )
    [4] => Array ( 
        [id] => 20 
        [name] => chocolate powder 
        [type] => lipid 
        [measurement] => Tablespoon 
        [amount] => 2 
    )
    [5] => Array ( 
        [id] => 38 
        [name] => chocolate square 
        [type] => lipid 
        [measurement] => 
        [amount] => 1 
    ) 
) 

I tried the following :

$courses = [];
foreach ($array as $key => $item) {
    if (!key_exists($key, $courses)) {
        $courses[$key] = [];
    }
    foreach ($item as $row) {
        $id = $row['id'];
        if (!key_exists($id, $courses[$key])) {
             $courses[$key][$id] = $row;
             continue;
        }
        $r = &$courses[$key][$id];
        $r['amount'] += $row['amount'];
    }
}

Output:

Array (
    [Fruit] => Array (
        [17] => Array (
            [id] => 17
            [name] => seasonal fruit
            [type] => Fruit
            [measurement] => 
            [amount] => 3
        )
    )
    [protein] => Array (
        [16] => Array (
            [id] => 16
            [name] => soy yogurt
            [type] => protein
            [measurement] => 
            [amount] => 2
        )
        [18] => Array (
            [id] => 18
            [name] => oilseed
            [type] => protein
            [measurement] => grip
            [amount] => 3
         )
     )
     [lipid] => Array (
         [19] => Array (
             [id] => 19
             [name] => coconut powder
             [type] => lipid
             [measurement] => Tablespoon
             [amount] => 2
         )
         [20] => Array (
             [id] => 20
             [name] => chocolate powder
             [type] => lipid
             [measurement] => Tablespoon
             [amount] => 2
          )
          [38] => Array (
              [id] => 38
              [name] => chocolate square
              [type] => lipid
              [measurement] => 
              [amount] => 1
          )
      )
  )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
211 views
Welcome To Ask or Share your Answers For Others

1 Answer

You were not far off, but everything you are interested in ins inside the inner foreach, that way you dont get the Fruit and Protien etc involved

$courses = [];
foreach( $array as $sub){
    foreach ($sub as $item) {
        if ( array_key_exists($item['id'], $courses) ) {
            // dup, so just add to the amount
            $courses[$item['id']]['amount'] += $item['amount'];
        } else {
            //New entry
            $courses[$item['id']] = $item;
        }
    }
}

// if its important to have indexes starting at 0 for the result
$courses = array_values($courses);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...