$ap_data) { $ap_prices = []; $ap_prices_avg = []; foreach ($ap_data as $ap_entry) { if ($ap_entry['his_price']) { $ap_prices[substr($ap_entry['his_date'], 0, 7)][] = $ap_entry['his_price']; } } $ap_prices[substr($ap_entry['ap_date'], 0, 7)][] = $ap_entry['ap_price']; foreach ($ap_prices as $date => $date_prices) { $ap_prices_avg[$date] = ceil(array_sum($date_prices)/count($date_prices)); } foreach ($ap_prices_avg as $date => $price) { $result_price[$date][$ap_entry['room_type_id']][] = $price; } if ($ap_entry['is_sold'] == 1) { $result_quantity[$date][$ap_entry['room_type_id']][] = 1; } } // avg current data foreach ($result_price as $date => $types_data) { foreach ($types_data as $type => $prices) { $res1['price'][$date][$type] = ceil(array_sum($prices)/count($prices)); } } foreach ($result_quantity as $date => $types_data) { foreach ($types_data as $type => $quantities) { $res1['quantity'][$date][$type] = array_sum($quantities); } } // get parsed data $sql = "SELECT block_id, room_type_id, price, period, quantity FROM nb_sales WHERE block_id=" . $block_id . " ORDER BY period"; $rez = mysql_query($sql); while ($obj = mysql_fetch_assoc($rez)) { $res2['price'][substr($obj['period'], 0, 7)][$obj['room_type_id']] = $obj['price']; $res2['quantity'][substr($obj['period'], 0, 7)][$obj['room_type_id']] = $obj['quantity']; } //get date periods, and room types $price_dates = []; $quantity_dates = []; $price_types = []; $quantity_types = []; if (!empty($res1)) { foreach ($res1['price'] as $key => $date) { if (array_search($key, $price_dates) === FALSE) { $price_dates[] = $key; } foreach ($date as $type => $value) { if (array_search($type, $price_types) === FALSE) { $price_types[] = $type; } } } foreach ($res1['quantity'] as $key => $date) { if (array_search($key, $quantity_dates) === FALSE) { $quantity_dates[] = $key; } foreach ($date as $type => $value) { if (array_search($type, $quantity_types) === FALSE) { $quantity_types[] = $type; } } } } if (!empty($res2)) { foreach ($res2['price'] as $key => $date) { if (array_search($key, $price_dates) === FALSE) { $price_dates[] = $key; } foreach ($date as $type => $value) { if (array_search($type, $price_types) === FALSE) { $price_types[] = $type; } } } foreach ($res2['quantity'] as $key => $date) { if (array_search($key, $quantity_dates) === FALSE) { $quantity_dates[] = $key; } foreach ($date as $type => $value) { if (array_search($type, $quantity_types) === FALSE) { $quantity_types[] = $type; } } } } sort($price_dates); reset($price_dates); sort($quantity_dates); reset($quantity_dates); //foreach dates and room types //$price_dates //$price_types //$quantity_dates //$quantity_types $result = []; foreach ($price_dates as $date) { $prices = []; $prices['date'] = $date; foreach ($price_types as $type) { if (isset($res1['price'][$date][$type]) && isset($res2['price'][$date][$type])) { $prices[$type] = ceil(($res1['price'][$date][$type] + $res2['price'][$date][$type]) / 2); } elseif (isset($res1['price'][$date][$type])) { $prices[$type] = $res1['price'][$date][$type]; } elseif (isset($res2['price'][$date][$type])) { $prices[$type] = $res2['price'][$date][$type]; } else { $prices[$type] = 0; } } $result['price'][] = $prices; } foreach ($quantity_dates as $date) { $quantities = []; $quantities['date'] = $date; foreach ($quantity_types as $type) { if (isset($res1['quantity'][$date][$type]) && isset($res2['quantity'][$date][$type])) { $quantities[$type] = $res1['quantity'][$date][$type] + $res2['quantity'][$date][$type]; } elseif (isset($res1['quantity'][$date][$type])) { $quantities[$type] = $res1['quantity'][$date][$type]; } elseif (isset($res2['quantity'][$date][$type])) { $quantities[$type] = $res2['quantity'][$date][$type]; } else { $quantities[$type] = 0; } } $result['count'][] = $quantities; } echo json_encode($result); exit; } echo false; exit;