-1

The array given below shows the number of different models of mobile phones sold by different companies, in a shop for a week of 2017. You have to find the total number of mobile phones sold irrespective of models for each companies on that period in below format. Array (SAMSUNG => 9341, XIAOMI => 4807, ...)

$mobiles_sold = array( 
        '1_DAY_2017' => array('SAMSUNG' => array(549, 199, 999),'XIAOMI' => array(199, 2999, 499)), 
        '2_DAY_2017' => array('SAMSUNG' => array(699, 999),'LENOVO' => array(280, 2550, 849)), 
        '3_DAY_2017' => array('OPPO' => array(500, 599),'SAMSUNG' => array(799)), 
        '4_DAY_2017' => array('SAMSUNG' => array(1299, 499, 799, 2500),'OPPO' => array(299, 349, 499)), 
        '5_DAY_2017' => array('XIAOMI' => array(500, 270, 340), 'VIVO' => array(4599, 299)),
        '6_DAY_2017' => array('VIVO' => array(240, 1899, 759, 530),'OPPO' => array(999)), 
        '7_DAY_2017' => array('OPPO' => array(300, 252, 1290), 'LENOVO' => array(570, 1300, 666)), );
Oskyk
  • 448
  • 3
  • 12

2 Answers2

0

Okay. :) You may print them to the console if... You make a loop and print them to the console is that what you want?

for (i = 0, i < SamsungArray.Length, i++){
    Debug.Log(SamsungArray[i]);
    i = i + 1;
}
0

You could use a foreach loop and using array_sum:

$result = [];

foreach ($mobiles_sold as $day) {
    foreach ($day as $key => $value) {
        if (!array_key_exists($key, $result)) {
            $result[$key] = array_sum($value);
            continue;
        }
        $result[$key] += array_sum($value);
    }
}

Demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70