There are two arrays, one contains unique values, and the second has many entries with identical headers - the name of the services and the cost. I want to add up all the amounts and withdraw the positions for the period in order to get the result:

  • the ________ service was provided ___ times, for a total of ____ p.

Array of data to be calculated

$mv_today[] = array($s_name => array('vol' => $s_vol, 'price' => $u_price)); 

Data is stored as:

Body wash including thresholds: 1: 350 / Dry fog: 3: 700 /

Name: category: cost / ... It turns out I first pull everything out of the base, then on the fly form a report for the past day / month.

Array with unique values:

 $uslugi[] = $s_name; 

Everything is simple - the name of the service, nothing more.


I somehow managed to make arrays myself, but I cannot connect them.


 $result_today = mysql_query("SELECT * FROM history WHERE start_work BETWEEN '$start_date_today' AND '$stop_date_today' ORDER BY `history`.`start_work` DESC"); while ($row_today = mysql_fetch_array($result_today)){ $mv_today_all[] = $row_today[type]; //заносим всС значСния Π² массив (грязными) };//while foreach ($mv_today_all as $key => $value) { $str_value = explode("/", $value); foreach($str_value as $str_val){ list($u_name, $u_cat, $u_price) = explode(":", $str_val); if ($u_name){ $s_vol = "1"; $s_name = $u_name."#".$u_cat; $mv_today[] = array($s_name => array('vol' => $s_vol, 'price' => $u_price)); $uslugi[] = $s_name; }//if ($u_name) }//foreach($str_value }//foreach ($mv_today_all $uslugi = array_unique($uslugi); 
  • show the array itself or both of the arrays that we understood - L. Vadim
  • Array ([0] => Body washing including thresholds and purging # 1 [1] => Dry mist # 3 [2] => Abrasive polishing # 2 [3] => Liquid glass # 3) - Dimastik86
  • Array ([0] => Array ([Body wash including thresholds and purge # 1] => Array ([vol] => 1 [price] => 350)) [1] => Array ([Dry fog # 3] => Array ([vol] => 1 [price] => 700)) [2] => Array ([Abrasive polishing # 2] => Array ([vol] => 1 [price] => 10000)) [ 3] => Array ([Liquid Glass # 3] => Array ([vol] => 1 [price] => 10000)) [4] => Array ([Abrasive Polishing # 2] => Array ([vol] => 1 [price] => 10000)) [5] => Array ([Body wash including thresholds and purge # 1] => Array ([vol] => 1 [price] => 350))) - Dimastik86
  • in general, I thought of doing the third massi, in which, after checking the uniqueness of the record, if not, then we simply write, if there is, we add to vol + 1 and add the sum to the sum - Dimastik86
  • And those. You deleted the previous question and my answer with him? :) - Rochfort

2 answers 2

 $mv_today = array($s_name => array('vol' => 12, 'price' => 34)); foreach ($mv_today as $value) { echo $value['vol'] * $value['price']; } 

Check for uniqueness, displays only unique values.

 array_unique($mv_today) 

Connect 2 arrays

 array_merge($array1, $array2); 

Array filtering, search for duplicates, additions by cost and total

 $mv_today[] = array('Мойка ΠΊΡƒΠ·ΠΎΠ²Π° Π²ΠΊΠ»ΡŽΡ‡Π°Ρ ΠΏΠΎΡ€ΠΎΠ³ΠΈ ΠΈ ΠΏΡ€ΠΎΠ΄ΡƒΠ²ΠΊΠ°#1' => array('vol' => 1, 'price' => 350)); $mv_today[] = array('Абразивная ΠΏΠΎΠ»ΠΈΡ€ΠΎΠ²ΠΊΠ°#2' => array('vol' => 1, 'price' => 10000)); $mv_today[] = array('Π‘ΡƒΡ…ΠΎΠΉ Ρ‚ΡƒΠΌΠ°Π½#3' => array('vol' => 1, 'price' => 700)); $mv_today[] = array('Абразивная ΠΏΠΎΠ»ΠΈΡ€ΠΎΠ²ΠΊΠ°#2' => array('vol' => 1, 'price' => 10000)); $mv_today[] = array('Π‘ΡƒΡ…ΠΎΠΉ Ρ‚ΡƒΠΌΠ°Π½#3' => array('vol' => 1, 'price' => 700)); //print_r($mv_today); $tmp = []; for ($i=0; $i < count($mv_today); $i++) { foreach ($mv_today[$i] as $key => $value) { if(!isset($tmp[$key])) { $tmp[$key] = $value; } else { $tmp[$key]['vol'] = $tmp[$key]['vol'] + $value['vol']; $tmp[$key]['price'] = $tmp[$key]['price'] + $value['price']; } } } print_r($tmp); 
  • I have already learned how to display, but how to cross? I'm more interested in checking for uniqueness, replacing an existing entry and deleting an old one - Dimastik86
  • I added the answer - L. Vadim
  • array_unique ($ mv_today) I think will not work, because it will kill some of the lines that I need to count - Dimastik86
  • he deletes the same records - L. Vadim
  • so this is funny, we provide the same price services during the day, there may be half the same - Dimastik86

Implemented like this, maybe someone will need

 $result_today = mysql_query("SELECT * FROM history WHERE start_work BETWEEN '$start_date_today' AND '$stop_date_today' ORDER BY `history`.`start_work` DESC"); while ($row_today = mysql_fetch_array($result_today)){ $str_value = explode("/", $row_today[type]); foreach($str_value as $str_val){ list($u_name, $u_cat, $u_price) = explode(":", $str_val); if ($u_name){ $s_name = $u_name."#".$u_cat; if(!isset($mv_today[$s_name])){ $mv_today[$s_name] = array('price' => $u_price, 'vol' => 1); }else{ $u_price = $mv_today[$s_name]['price']+$u_price; $u_vol = $mv_today[$s_name]['vol']+1; $mv_today[$s_name] = array('price' => $u_price, 'vol' => $u_vol); };//if(!isset($mv_today[$s_name]) };//if ($u_name) };//foreach };//while foreach($mv_today as $key => $today){ print $key." (".$today[vol].") Π½Π° сумму ".$today[price]."<br>"; }