combine and add data in one array index? - php
i have a array :
Array
(
[0] => Array
(
[spid] => 20
[name] => Sunny
[clientid] => 15
[selectedproduct] => ["190","191"]
[memberid] => 637
[partner_id] => 390
[monthly_forecast] => a:2:{i:0;s:151:"[["1","0","0","1","0","0","1","0","1","0","0","0"],["1","0","0","0","1","0","0","0","1","0","1","1"],["1","0","1","0","1","0","1","0","1","0","0","1"]]";i:1;s:151:"[["1","0","1","0","1","0","1","0","1","0","1","0"],["1","0","1","0","1","1","0","1","0","1","0","1"],["1","0","1","0","1","0","1","1","1","0","1","1"]]";}
[growth_plan] => {"product":["190","191"],"plans":["8","8"]}
[other_palns] => {"product":["190","191"],"plans":["8","8"],"total":["4000","5000"]}
[marketing_s_values] => {"190":"2400","191":"7200"}
[marketing_s_values_monthly] => {"190":[{"1":"150","2":"150","3":"150","4":"150","5":"150","6":"150","7":"150","8":"150","9":"150","10":"150","11":"150","12":"150"},{"1":"200","2":"200","3":"200","4":"200","5":"200","6":"200","7":"200","8":"200","9":"200","10":"200","11":"200","12":"200"},{"1":"250","2":"250","3":"250","4":"250","5":"250","6":"250","7":"250","8":"250","9":"250","10":"250","11":"250","12":"250"},{"1":"300","2":"300","3":"300","4":"300","5":"300","6":"300","7":"300","8":"300","9":"300","10":"300","11":"300","12":"300"},{"1":"350","2":"350","3":"350","4":"350","5":"350","6":"350","7":"350","8":"350","9":"350","10":"350","11":"350","12":"350"}],"191":[{"1":"500","2":"500","3":"500","4":"500","5":"500","6":"500","7":"500","8":"500","9":"500","10":"500","11":"500","12":"500"},{"1":"600","2":"600","3":"600","4":"600","5":"600","6":"600","7":"600","8":"600","9":"600","10":"600","11":"600","12":"600"},{"1":"700","2":"700","3":"700","4":"700","5":"700","6":"700","7":"700","8":"700","9":"700","10":"700","11":"700","12":"700"},{"1":"800","2":"800","3":"800","4":"800","5":"800","6":"800","7":"800","8":"800","9":"800","10":"800","11":"800","12":"800"},{"1":"900","2":"900","3":"900","4":"900","5":"900","6":"900","7":"900","8":"900","9":"900","10":"900","11":"900","12":"900"}]}
)
[1] => Array
(
[spid] => 20
[name] => Sunny
[clientid] => 15
[selectedproduct] => ["199","210"]
[memberid] => 641
[partner_id] => 394
[monthly_forecast] => a:2:{i:0;s:163:"[["10","0","0","0","30","0","0","60","0","0","0","0"],["20","0","0","30","0","0","20","0","0","0","50","0"],["30","0","0","0","20","0","0","30","0","20","0","30"]]";i:1;s:154:"[["20","0","0","0","0","0","0","0","0","0","0","0"],["30","0","0","0","0","0","0","0","0","0","0","0"],["40","0","0","0","0","0","0","0","0","0","0","0"]]";}
[growth_plan] => {"product":["199","210"],"plans":["8","8"]}
[other_palns] => {"product":["199","210"],"plans":["10","10"],"total":["6000","3000"]}
[marketing_s_values] => {"199":"4700","210":"0"}
[marketing_s_values_monthly] => {"199":[{"1":"4600","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"4700","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"4800","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"4900","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"5000","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"}],"210":[{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"}]}
)
[2] => Array
(
[spid] => 20
[name] => Sunny
[clientid] => 15
[selectedproduct] => ["190","191","192","193"]
[memberid] => 574
[partner_id] => 374
[monthly_forecast] => a:4:{i:0;s:151:"[["1","0","0","1","0","0","1","0","1","0","0","0"],["1","0","0","0","1","0","0","0","1","0","1","1"],["1","0","1","0","1","0","1","0","1","0","0","1"]]";i:1;s:151:"[["1","0","1","0","1","0","1","0","1","0","1","0"],["1","0","1","0","1","1","0","1","0","1","0","1"],["1","0","1","0","1","0","1","1","1","0","1","1"]]";i:2;s:151:"[["1","1","1","0","1","1","1","0","0","0","1","1"],["1","1","1","0","0","1","1","1","1","0","1","1"],["1","1","1","1","0","0","1","1","1","1","1","1"]]";i:3;s:151:"[["1","0","1","0","0","0","1","0","1","0","0","2"],["1","0","0","2","1","0","1","0","1","1","0","1"],["1","0","2","1","1","1","0","1","0","1","1","1"]]";}
[growth_plan] => {"product":["190","191","192","193"],"plans":["8","8","9","8"]}
[other_palns] => {"product":["190","191","192","193"],"plans":["8","8","8","8"],"total":["4000","5000","1500","36000"]}
[marketing_s_values] => {"190":"2400","191":"7200","192":"2400","193":"24000"}
[marketing_s_values_monthly] => {"190":[{"1":"150","2":"150","3":"150","4":"150","5":"150","6":"150","7":"150","8":"150","9":"150","10":"150","11":"150","12":"150"},{"1":"200","2":"200","3":"200","4":"200","5":"200","6":"200","7":"200","8":"200","9":"200","10":"200","11":"200","12":"200"},{"1":"250","2":"250","3":"250","4":"250","5":"250","6":"250","7":"250","8":"250","9":"250","10":"250","11":"250","12":"250"},{"1":"300","2":"300","3":"300","4":"300","5":"300","6":"300","7":"300","8":"300","9":"300","10":"300","11":"300","12":"300"},{"1":"350","2":"350","3":"350","4":"350","5":"350","6":"350","7":"350","8":"350","9":"350","10":"350","11":"350","12":"350"}],"191":[{"1":"500","2":"500","3":"500","4":"500","5":"500","6":"500","7":"500","8":"500","9":"500","10":"500","11":"500","12":"500"},{"1":"600","2":"600","3":"600","4":"600","5":"600","6":"600","7":"600","8":"600","9":"600","10":"600","11":"600","12":"600"},{"1":"700","2":"700","3":"700","4":"700","5":"700","6":"700","7":"700","8":"700","9":"700","10":"700","11":"700","12":"700"},{"1":"800","2":"800","3":"800","4":"800","5":"800","6":"800","7":"800","8":"800","9":"800","10":"800","11":"800","12":"800"},{"1":"900","2":"900","3":"900","4":"900","5":"900","6":"900","7":"900","8":"900","9":"900","10":"900","11":"900","12":"900"}],"192":[{"1":"100","2":"100","3":"100","4":"100","5":"100","6":"100","7":"100","8":"100","9":"100","10":"100","11":"100","12":"100"},{"1":"200","2":"200","3":"200","4":"200","5":"200","6":"200","7":"200","8":"200","9":"200","10":"200","11":"200","12":"200"},{"1":"300","2":"300","3":"300","4":"300","5":"300","6":"300","7":"300","8":"300","9":"300","10":"300","11":"300","12":"300"},{"1":"400","2":"400","3":"400","4":"400","5":"400","6":"400","7":"400","8":"400","9":"400","10":"400","11":"400","12":"400"},{"1":"500","2":"500","3":"500","4":"500","5":"500","6":"500","7":"500","8":"500","9":"500","10":"500","11":"500","12":"500"}],"193":[{"1":"1500","2":"1500","3":"1500","4":"1500","5":"1500","6":"1500","7":"1500","8":"1500","9":"1500","10":"1500","11":"1500","12":"1500"},{"1":"2000","2":"2000","3":"2000","4":"2000","5":"2000","6":"2000","7":"2000","8":"2000","9":"2000","10":"2000","11":"2000","12":"2000"},{"1":"2200","2":"2200","3":"2200","4":"2200","5":"2200","6":"2200","7":"2200","8":"2200","9":"2200","10":"2200","11":"2200","12":"2200"},{"1":"2500","2":"2500","3":"2500","4":"2500","5":"2500","6":"2500","7":"2500","8":"2500","9":"2500","10":"2500","11":"2500","12":"2500"},{"1":"2700","2":"2700","3":"2700","4":"2700","5":"2700","6":"2700","7":"2700","8":"2700","9":"2700","10":"2700","11":"2700","12":"2700"}]}
)
In this array as you see "spid" is similar to in all the array index.i just want that it will merge all the data in one index array.
As you see "selectedproduct" of index 0 and index 2 is similar data like 190 and 191 whenever the "selectedproduct" is similar i just want that there respective "monthly_forecast" values added with each other and merge all the three index values into one but only in the case where there "selectedproduct" is similar only those "monthly_forecast" value added.
i tried this code:
$new_values = array();
foreach($get_partner_same_forecast as $value) {
if(isset($new_values[$value['spid']])) {
$temp = $new_values[$value['spid']];
$temp['selectedproduct'] .= ',' . $value['selectedproduct'];
$user[] = json_decode($value['selectedproduct'],true);
$result = array();
foreach($user as $item) {
$result = array_merge($result, $item);
}
$temp['selectedproduct']=json_encode($result);
$temp['partner_acc_name'] .= ',' . $value['partner_acc_name'];//mergepartner name
$temp['crm_acc_name'] .= ',' . $value['crm_acc_name']; //merge crmname
$temp['memberid'] .= ',' . $value['memberid'];//merge memberid
$temp['partner_id'] .= ',' . $value['partner_id'];//merge partnerid
$temp['growth_plan'] .= ',' . $value['growth_plan'];//merge growth plan
$user1[] = json_decode($value['growth_plan'],true);
$product = array();
$plan = array();
foreach($user1 as $k=>$v)
{
$product = array_merge($product, $v['product']);
$plan = array_merge($plan, $v['plans']);
$user1[$k]['product'] = $product;
$user1[$k]['plans'] = $plan;
}
$temp['growth_plan']=json_encode($user1[1]);
$temp['monthly_forecast'] .= ',' . $value['monthly_forecast'];//merge unserialize monthly data
$data1=array();
$data2=array();
$data2=unserialize($value['monthly_forecast']);
$data1=unserialize($temp['monthly_forecast']);
$combinedData=array();
$combinedData = array($data1, $data2);
$monthly_forecast = array();
$monthly_forecast=serialize($combinedData);
$temp['monthly_forecast']=$monthly_forecast;//merge monthly
$monthly_forecast=unserialize($monthly_forecast);
//echo "<pre>"; print_r($monthly_forecast);echo "</pre>";
$new_values[$value['spid']] = $temp;
} else {
$new_values[$value['spid']] = $value;
}
}
$new_values = array_values($new_values);
Related
i am facing problem with some logical error
I want to sum the individual student number with some condition. The Condition is if a student fails any one subject then it will give me 0 value not sum any of his number. When used this condition it will give me wrong value.now these the problem I faced and I tried lots of things but not working. In the below, I give you code and the output and also the output I want. controller: if (!empty($examSchedule)) { $new_array = array(); foreach ($studentList as $stu_key => $stu_value) { $array = array(); $array['student_id'] = $stu_value['id']; $array['admission_no'] = $stu_value['admission_no']; $array['roll_no'] = $stu_value['roll_no']; $array['firstname'] = $stu_value['firstname']; $array['lastname'] = $stu_value['lastname']; $array['dob'] = $stu_value['dob']; $array['father_name'] = $stu_value['father_name']; $x = array(); foreach ($examSchedule as $ex_key => $ex_value) { $exam_array = array(); $exam_array['exam_schedule_id'] = $ex_value['id']; $exam_array['exam_id'] = $ex_value['exam_id']; $exam_array['subject_id'] = $ex_value['subject_id']; $exam_array['full_marks'] = $ex_value['full_marks']; $exam_array['passing_marks'] = $ex_value['passing_marks']; $exam_array['exam_name'] = $ex_value['name']; $exam_array['exam_type'] = $ex_value['type']; $student_exam_result = $this->examresult_model->get_exam_result($ex_value['id'], $stu_value['id']); $exam_array['attendence'] = $student_exam_result->attendence; $exam_array['get_marks'] = $student_exam_result->get_marks; $x[] = $exam_array; } $array['exam_array'] = $x; $new_array[] = $array; } $data['examSchedule'] = $new_array; } if ($this->input->post('save_exam') == "save_exam") { $ex_array = array(); $exam_id = $this->input->post('exam_id'); $student_array = $this->input->post('student'); $exam_array = $this->input->post('exam_schedule'); $total_marks = array(); foreach ($student_array as $key => $student) { foreach ($examSchedule as $ex_key => $ex_value) { foreach ($exam_array as $key => $exam) { $record['get_marks'] = 0; $record['attendence'] = "pre"; if ($this->input->post('student_absent' . $student . "_" . $exam) == "") { $get_marks = $this->input->post('student_number' . $student . "_" . $exam); $record['get_marks'] = $get_marks; $passing_marks = $ex_value['passing_marks']; if ($get_marks != '0' && $get_marks>=$passing_marks) { $total_marks[$student] += $get_marks; } else { $total_marks[$student] = 0; break; } } else { $record['attendence'] = $this->input->post('student_absent' . $student . "_" . $exam); } $record['exam_schedule_id'] = $exam; $record['student_id'] = $student; $record['exam_id'] = $exam_id; $inserted_id = $this->examresult_model->add_exam_result($record); if ($inserted_id) { $ex_array[$student] = $exam_id; } } $total['total_mark'] = $total_marks[$student]; $total['exam_id'] = $this->input->post('exam_id'); $total['student_id'] = $student; $total['class_id'] = $class_id; $total['section_id'] = $section_id; $total['year'] = date("Y"); $total_mark = $this->examresult_model->add_total_result($total); } echo "<pre>"; print_r( $total['total_mark']); } exit(); if (!empty($ex_array)) { $this->mailsmsconf->mailsms('exam_result', $ex_array, NULL, $exam_array); } redirect('admin/mark'); } output(i get): 3213 3171 1248 2989 4291 0 0 0 0 0 0 0 0 output(i want): 459 453 416 0 //(427 is the sum but one subject fail thatswhy it sum zero) 613 0 0 0 0 0 0 0 0 value of student_array (these all are student id): Array( [0] => 110 [1] => 111 [2] => 120 [3] => 121 [4] => 112 [5] => 113 [6] => 114 [7] => 122 [8] => 115 [9] => 116 [10] => 117 [11] => 118 [12] => 119 ) value of exam_array: Array ( [0] => 84 [1] => 85 [2] => 86 [3] => 87 [4] => 88 [5] => 89 [6] => 90 ) value of examSchedule: Array ( [0] => Array ( [student_id] => 110 [admission_no] => 01 [roll_no] => 01 [firstname] => Md. Abdur [lastname] => Rahaman [dob] => 2015-05-12 [father_name] => Sowpan Chow [exam_array] => Array ( [0] => Array ( [exam_schedule_id] => 84 [exam_id] => 7 [subject_id] => 4 [full_marks] => 100 [passing_marks] => 33 [exam_name] => Bangla [exam_type] => Theory [attendence] => pre [get_marks] => 75.00 ) [1] => Array ( [exam_schedule_id] => 85 [exam_id] => 7 [subject_id] => 1 [full_marks] => 100 [passing_marks] => 33 [exam_name] => English [exam_type] => Theory [attendence] => pre [get_marks] => 82.00 ) [2] => Array ( [exam_schedule_id] => 86 [exam_id] => 7 [subject_id] => 2 [full_marks] => 100 [passing_marks] => 33 [exam_name] => Math [exam_type] => Theory [attendence] => pre [get_marks] => 88.00 ) [3] => Array ( [exam_schedule_id] => 87 [exam_id] => 7 [subject_id] => 3 [full_marks] => 100 [passing_marks] => 33 [exam_name] => Religion [exam_type] => Theory [attendence] => pre [get_marks] => 87.00 ) [4] => Array ( [exam_schedule_id] => 88 [exam_id] => 7 [subject_id] => 10 [full_marks] => 50 [passing_marks] => 16 [exam_name] => Art & Craft [exam_type] => Theory [attendence] => pre [get_marks] => 45.00 ) [5] => Array ( [exam_schedule_id] => 89 [exam_id] => 7 [subject_id] => 9 [full_marks] => 50 [passing_marks] => 16 [exam_name] => General Knowledge [exam_type] => Theory [attendence] => pre [get_marks] => 42.00 ) [6] => Array ( [exam_schedule_id] => 90 [exam_id] => 7 [subject_id] => 11 [full_marks] => 50 [passing_marks] => 16 [exam_name] => Computer [exam_type] => Theory [attendence] => pre [get_marks] => 40.00 ) ) ) [1] => Array ( #value ) [2] => Array ( #value ) [3] => Array ( #value ) [4] => Array ( #value ) [5] => Array ( #value ).. [12] => Array ( #value )
you could make a separate foreach loop to get the whole marks of a single student, then check if it contains any 0 mark on it before doing a passing mark validation/additions : if ($this->input->post('save_exam') == "save_exam") { $ex_array = array(); $exam_id = $this->input->post('exam_id'); $student_array = $this->input->post('student'); $exam_array = $this->input->post('exam_schedule'); $total_marks = array(); foreach ($student_array as $key => $student) { // check if current student having 0 mark $student_mark = []; foreach ($exam_array as $key => $exam) { $student_mark[$student][] = $this->input->post('student_number' . $student . "_" . $exam); } $zero_mark_flag = in_array(0, $student_mark[$student], true) ? true : false; // return true if contains 0 mark foreach ($examSchedule as $ex_key => $ex_value) { foreach ($exam_array as $key => $exam) { $record['get_marks'] = 0; $record['attendence'] = "pre"; if ($this->input->post('student_absent' . $student . "_" . $exam) == "") { $get_marks = $this->input->post('student_number' . $student . "_" . $exam); $record['get_marks'] = $get_marks; $passing_marks = $ex_value['passing_marks']; // there are 0 on one or more student marks, so skip the mark addition if ($zero_mark_flag === true) { $total_marks[$student] = 0; break; } else if ($get_marks != '0' && $get_marks>=$passing_marks) { $total_marks[$student] += $get_marks; } } else { $record['attendence'] = $this->input->post('student_absent' . $student . "_" . $exam); } $record['exam_schedule_id'] = $exam; $record['student_id'] = $student; $record['exam_id'] = $exam_id; $inserted_id = $this->examresult_model->add_exam_result($record); if ($inserted_id) { $ex_array[$student] = $exam_id; } } $total['total_mark'] = $total_marks[$student]; $total['exam_id'] = $this->input->post('exam_id'); $total['student_id'] = $student; $total['class_id'] = $class_id; $total['section_id'] = $section_id; $total['year'] = date("Y"); $total_mark = $this->examresult_model->add_total_result($total); } echo "<pre>"; print_r( $total['total_mark']); } exit(); if (!empty($ex_array)) { $this->mailsmsconf->mailsms('exam_result', $ex_array, NULL, $exam_array); } redirect('admin/mark'); } So the student mark will always set to 0.
How to remove the duplicate data into array [duplicate]
This question already has answers here: Filter/Remove rows where column value is found more than once in a multidimensional array (4 answers) Closed 9 months ago. Need help for the code. Here is my list of array which i want to remove the two (2) Wi-fi. Array ( [20-10] => Array ( [facilityCode] => 20 [facilityGroupCode] => 10 [order] => 1 [number] => 1968 [voucher] => [description] => Year of construction ) [550-70] => Array ( [facilityCode] => 550 [facilityGroupCode] => 70 [order] => 1 [indFee] => [indYesOrNo] => 1 [voucher] => [description] => Wi-fi ) [20-60] => Array ( [facilityCode] => 20 [facilityGroupCode] => 60 [order] => 1 [indLogic] => 1 [voucher] => [description] => Shower ) [261-60] => Array ( [facilityCode] => 261 [facilityGroupCode] => 60 [order] => 1 [indFee] => [indYesOrNo] => 1 [voucher] => [description] => Wi-fi ) ) I do also tried the array_unique(); here is the result: Array ( [0] => Year of construction [1] => Shower [2] => Wi-fi ) But i want to keep the facilityCode,facilityGroupCode,order,number etc. Thanks for any help.
One liner is all can do your requirement, $result = array_reverse(array_values(array_column(array_reverse($arr), null, 'description'))); Source link for your requirement.
//populate data $mainArr = array(); $first = array( "facilityCode" => 20, "facilityGroupCode" => 10, "order" => 1, "number" => 1968, "voucher" => "", "description" => "Year of construction", ); $second = array( "facilityCode" => 550, "facilityGroupCode" => 70, "order" => 1, "indFee" => "", "indYesOrNo" => 1, "voucher" => "", "description" => "Wi-fi" ); $mainArr["20-10"] = $first; $mainArr["550-70"] = $second; $mainArr["261-60"] = $second; //get duplicates $counter = 0; $duplicates = array(); foreach ($mainArr as $key=>$val) { $counter++; if (in_array($key, $duplicates)) continue; $i = 0; foreach ($mainArr as $key1=>$val1) { if ($i < $counter) { $i++; continue; } if ($val["description"] == $val1["description"]) { array_push($duplicates, $key1); } } } //remove duplicates foreach($duplicates as $key) { unset($mainArr[$key]); }
$itemRows = array(); // Your main array $descriptionValues = array(); foreach ($itemRows as $itemKey => $itemRow) { foreach ($itemRow as $key => $value) { if ($key == 'description') { if (in_array($value, $descriptionValues)) { unset($itemRows[$itemKey]); continue 2; } $descriptionValues[] = $value; } } }
php array flip value as key and make it simple
i have been troubling to format array correctly i have this code: require('simple_html_dom.php'); $table = array(); $html = file_get_html('apc.html'); foreach($html->find('tr') as $row) { $rack = ltrim($row->find('td',0)->plaintext); $location = ltrim($row->find('td',1)->plaintext); $usage = ltrim($row->find('td',2)->plaintext); $auk = ltrim($row->find('td',3)->plaintext); $cost = ltrim($row->find('td',4)->plaintext); $rack = rtrim($rack); $location = rtrim($location); $usage = rtrim($usage); $auk = rtrim($auk); $cost = rtrim($cost); $table[$rack][$usage][$auk][$cost] = true; } echo '<pre>'; print_r($table); echo '</pre>'; using simple_html_dom above i can convert html table to an array as follow: [Rack01] => Array ( [741,60] => Array ( [1.409,04] => Array ( [267,72] => 1 ) ) [110,88] => Array ( [210,67] => Array ( [40,03] => 1 ) ) ) [Rack 09] => Array ( [843,84] => Array ( [1.603,30] => Array ( [304,63] => 1 ) ) ) I would like to have result as below: array( [0] => array ( [usage] => 'Rack 01', [usage] => '741,60', [auk] => '1.409.04', [cost] => '267,72') [1] => array ( [usage] => 'Rack 02', [usage] => 'value???', [auk] => 'value???', [cost] => 'value???') any help would be apreaciate
Something like this. Also note that trim will do both left and right: foreach($html->find('tr') as $row) { $rack = trim($row->find('td',0)->plaintext); $location = trim($row->find('td',1)->plaintext); $usage = trim($row->find('td',2)->plaintext); $auk = trim($row->find('td',3)->plaintext); $cost = trim($row->find('td',4)->plaintext); $table[] = array('rack' => $rack, 'usage' => $usage, 'auk' => $auk, 'cost' => $cost); }
How to group keys and values of an (sub)array and sum its values using PHP? [duplicate]
This question already has answers here: Group array data on one column and sum data from another column (5 answers) Closed 9 months ago. I have the following array Array ( [0] => Array ( [0] => ALFA [1] => 213 ) [1] => Array ( [0] => ALFA [1] => 151 ) [2] => Array ( [0] => ALFA [1] => 197 ) [3] => Array ( [0] => BETA [1] => 167 ) [4] => Array ( [0] => ZETA [1] => 254 ) [5] => Array ( [0] => GAMA [1] => 138 ) [6] => Array ( [0] => GAMA [1] => 213 ) ) And I would like to group the key[0] of the subarray so I can see how many equal keys it has. Something like that: ALFA => 3 BETA => 1 EPSI => 1 GAMA => 2 I tried with array_count_values, but without success. foreach ($array as $value) { echo '<pre>'; print_r(array_count_values($value)); echo '</pre>'; } With that we have following result: Array ( [ALFA] => 1 [213] => 1 ) Array ( [ALFA] => 1 [151] => 1 ) ... Array ( [GAMA] => 1 [213] => 1 ) And after that I would like to sum the values of each group as well. ALFA => 213 + 151 + 197 BETA => 167 ZETA => 254 GAMA => 138 + 213 I think that when we solve the first part of the problem, the second would follow easier with quite the same method. The final purpose is to divide the sum of values by the number of occurrences of each key group, so we can have an average of the values just like that: ALFA => (213+151+197) / 3 = 187 BETA => 167 ZETA => 254 GAMA => (138+213) / 2 = 175,5 This is not the main problem, but as I said, I'm stuck with the beginning of the solution and would appreciate any help.
I'm surprised at all the long and complicated answers. However, the initial foreach to model your data to something manageable is needed. After that you just need to do a really simple array_walk. <?php $result = array(); foreach ($array as $el) { if (!array_key_exists($el[0], $result)) { $result[$el[0]] = array(); } $result[$el[0]][] = $el[1]; } array_walk($result, create_function('&$v,$k', '$v = array_sum($v) / count($v);')); ?> Result: Array ( [ALFA] => 187 [BETA] => 167 [ZETA] => 254 [GAMA] => 175.5 )
Solution for you is here: Code: $input = [ ['alfa', 123], ['alfa', 223], ['alfa', 122], ['alfa', 554], ['alfa', 34], ['dalfa', 123], ['halfa', 223], ['dalfa', 122], ['halfa', 554], ['ralfa', 34] ]; $result = []; foreach ($input as $node) { if (isset($result[$node[0]])) { $result[$node[0]] = ['sum' => $result[$node[0]]['sum'] + $node[1], 'count' => $result[$node[0]]['count'] + 1]; } else { $result[$node[0]] = ['sum' => $node[1], 'count' => 1]; } } print_r($result); foreach ($result as $key => &$data) { $data = $data['sum'] / $data['count']; } print_r($result); Output: Array ( [alfa] => Array ( [sum] => 1056 [count] => 5 ) [dalfa] => Array ( [sum] => 245 [count] => 2 ) [halfa] => Array ( [sum] => 777 [count] => 2 ) [ralfa] => Array ( [sum] => 34 [count] => 1 ) ) Array ( [alfa] => 211.2 [dalfa] => 122.5 [halfa] => 388.5 [ralfa] => 34 )
$sort = array(); foreach ($array as $value) { $sort[$value[0]][] = $value[1]; } then you count how many keys each has $keys = array(); foreach($sort as $k => $v) { $keys[$k] = count($v); } then for calculating the amount $sum = array(); $average = array(); foreach($sort as $k => $v) { $amount = 0; foreach($v as $val) { $amount += $val; } $sum[$k] = $amount; $average[$k] = $amount / $keys[$k]; } HOWEVER, If you want all the details in one array: $final = array(); foreach ($array as $value) { $final[$value[0]]["values"][] = $value[1]; } foreach($final as $k => $v) { $final[$k]["amount"] = count($v['values']); $amount = 0; foreach($v['values'] as $val) { $amount += $val; } $final[$k]["sum"] = $amount; $final[$k]["average"] = $amount / $final[$k]["amount"]; } example: http://jdl-enterprises.co.uk/sof/25789697.php Includes Output
Just copy the codes to your favorite text editor, sure it works perfectly. $items = [ ['ALFA',213], ['ALFA',151], ['ALFA',197], ['BETA',167], ['ZETA',254], ['GAMA',138], ['GAMA',213] ]; echo '<pre>' . print_r($items,true) . '</pre>'; $result; foreach ($items as $value) { # code... if (isset($result[$value[0]])) { $sum = $result[$value[0]]['sum'] + $value[1]; $count = $result[$value[0]]['count'] + 1; $result[$value[0]] = ['sum' => $sum , 'count' => $count, 'divided' => ($sum / $count)]; } else { $result[$value[0]] = ['sum' => $value[1] , 'count' => 1 , 'divided' => ($value[1] / 1) ]; } } echo '<pre>' . print_r($result,true) . '</pre>';
$myArray = [ ["ALFA",213], ["ALFA",151], ["ALFA",197], ["BETA",167], ["ZETA",254], ["GAMA",138], ["GAMA",213] ]; $a1 = array(); //TEMPORARY ARRAY FOR KEEPING COUNT & TOTAL VALUES $res = array(); //ARRAY USED TO KEEP RESULT foreach($myArray as $val) { //AVOID PESKY NOTICES FOR UNDEFINED INDEXES if ( !array_key_exists($val[0],$a1) ) { $a1[$val[0]] = array("count" => 0,"total" => 0); $res[$val[0]] = 0; } //INCREMENT THE COUNT OF INSTANCES OF THIS KEY $a1[$val[0]]["count"]++; //INCREMENT THE TOTAL VALUE OF INSTANCES OF THIS KEY $a1[$val[0]]["total"]+=$val[1]; // UPDATE RESULT ARRAY $res[$val[0]] = $a1[$val[0]]["total"] / $a1[$val[0]]["count"]; } print_r($res); Should result in: Array ( [ALFA] => 187 [BETA] => 167 [ZETA] => 254 [GAMA] => 175.5 ) Sample: http://phpfiddle.org/lite/code/a7nt-5svf
How to output an array based on primary keys and foreign keys
Considering this array Array ( [0] => Array ( [id] => 51 [category_id] => 37 [title] => Sims ) [1] => Array ( [id] => 37 [category_id] => 26 [title] => Blackberry ) [2] => Array ( [id] => 26 [category_id] => 0 [title] => Mobile Device ) I would like to be able to print out: Mobile Device > Blackberry > Sims Based on the relationship between category_id and id.
Can you use the id as the key into the array? It will make your life a bit simpler. For example, if you define your array: Array ( [51] => Array ( [id] => 51 [category_id] => 37 [title] => Sims ) [37] => Array ( [id] => 37 [category_id] => 26 [title] => Blackberry ) [27] => Array ( [id] => 26 [category_id] => 0 [title] => Mobile Device ) Then you can write code like: //assume $a is your array, defined above //and that you have used the id for the array key $id = 51 do { print $a['title']; $id = $a['category_id']; }while($id != 0); EDIT: array_multisort probably isn't cleanest way to do this.
<?php $array = Array( array('id' => 51, 'category_id' => 37, 'title' => 'Sims'), array('id' => 37, 'category_id' => 26, 'title' => 'Blackberry'), array('id' => 26, 'category_id' => 0, 'title' => 'Mobile Device')); // First build an associative array ID->Object $map = array(); foreach( $array as $value ) { $map[$value['id']] = $value; } // Then build your path $path = array(); $value = $array[0]; while( true ) { $path[] = $value['title']; if( $value['category_id'] == 0 ) { break; } $value = $map[$value['category_id']]; if( !isset($value) ) { die("Data Inconsistency"); } } // Display path echo implode(array_reverse($path), ' > '); ?>
Try array_multisort()
Does your original array also contains entries that are to be left out? If not, use this: $sort_array = array(); foreach ($original_array as $key => $value) { $sort_array[] = $value['category_id']; } array_multisort($sort_array, SORT_ASC, $original_array); The above will sort $original_array based on the category_id index. If your array contains entries that have nothing to do with the rest, and you want to leave them out, you have to use something like this: // remap keys based on category_id $parts = array(); foreach ($original_array as $array) { $parts[$array['category_id']] = $array; } // build tree list $category_id = 0; $result = array(); while (isset($parts[$category_id])) { $result[] = $parts[$category_id]['title']; $category_id = $parts[$category_id]['id']; } echo implode(' > ', $result);