PHP foreach() last value not being compared - php

I have an array of values as such:
Array
(
[0] => Array
(
[product_colour_sizes_id] => 37105
[product_colour_sizes_product_id] => 810
[product_colour_sizes_option_value_colour_id] => 61
[product_colour_sizes_option_value_size_id] => 904
[quantity] => 999
[product_colour_sizes_name_sizes] => 16-165
[product_colour_sizes_name_colours] => Red
)
[1] => Array
(
[product_colour_sizes_id] => 37104
[product_colour_sizes_product_id] => 810
[product_colour_sizes_option_value_colour_id] => 61
[product_colour_sizes_option_value_size_id] => 905
[quantity] => 999
[product_colour_sizes_name_sizes] => 17-175
[product_colour_sizes_name_colours] => Red
)
[2] => Array
(
[product_colour_sizes_id] => 37103
[product_colour_sizes_product_id] => 810
[product_colour_sizes_option_value_colour_id] => 61
[product_colour_sizes_option_value_size_id] => 906
[quantity] => 999
[product_colour_sizes_name_sizes] => 185-19
[product_colour_sizes_name_colours] => Red
)
[3] => Array
(
[product_colour_sizes_id] => 37102
[product_colour_sizes_product_id] => 810
[product_colour_sizes_option_value_colour_id] => 61
[product_colour_sizes_option_value_size_id] => 907
[quantity] => 999
[product_colour_sizes_name_sizes] => 19.5
[product_colour_sizes_name_colours] => Red
)
)
I then have a foreach loop which iterates over the above data and reduces each array down to a single row corresponding to different [product_colour_sizes_name_sizes]. However i have a problem that the last array is not being compared. Here is my loop:
foreach($product_combinations as $key => $product_combination){
if($key > -1){
if($product_combinations[$key+1]['product_colour_sizes_name_colours']
== $product_combinations[$key]['product_colour_sizes_name_colours']
){
$color = $product_combinations[$key]['product_colour_sizes_name_colours'];
$color['name'] = $color;
$id = $product_combinations[$key]['product_colour_sizes_option_value_size_id'];
$sizes[$id] = $product_combinations[$key]['product_colour_sizes_name_sizes'];
$quantity = $product_combinations[$key]['quantity'];
$sizes['quantity'] = $quantity;
/* Dont forget theses*/
$sizes['product_colour_sizes_id'] = $product_combination['product_colour_sizes_id'];
$sizes['product_colour_sizes_product_id'] = $product_combination['product_colour_sizes_product_id'];
$sizes['product_colour_sizes_option_value_colour_id'] = $product_combination['product_colour_sizes_option_value_colour_id'];
$sizes["product_colour_sizes_option_value_size_id$x"] =$product_combination['product_colour_sizes_option_value_size_id'];
$x++;
} else {
array_push($colours,
array(
$color => $sizes
));
unset($sizes);
$x = 0;
}
}
}
Essentially now, my 4th ([3]) array is being compared as
$product_combinations[$key+1] == $product_combinations[$key]['product_colour_sizes_name_colours'] with $key+1 being an empty value.
I know the issue as stated above, but can't think of a good way to overcome it with my current logic, can anyone think of a solution?
Edit
What i am attempting to do is reduce the row down to this exact array structure (with the last array included of course that is currently not be added):
Array
(
[0] => Array
(
[Red] => Array
(
[904] => 16-165
[quantity] => 999
[product_colour_sizes_id] => 37103
[product_colour_sizes_product_id] => 810
[product_colour_sizes_option_value_colour_id] => 61
[product_colour_sizes_option_value_size_id0] => 904
[905] => 17-175
[product_colour_sizes_option_value_size_id1] => 905
[906] => 185-19
[product_colour_sizes_option_value_size_id2] => 906
)
)
)

A bit unclear on your original core logic, but based on your example output data, I believe the solution below should work.
The key is really understanding your final desired output because a lot of the logical loops the original code goes through can be eliminated by just using the magic of associative arrays. This solves the whole $key+1 matching $key issue—which would never get the last value due to the inherent flaw in $key+1 logic—as well as a few other things.
Anyway, here is an array of test values including extra values for a Blue color in addition to the Red; cleaned up code to follow:
// Test data array.
$product_combinations = array();
$product_combinations[0][product_colour_sizes_id] = 37105;
$product_combinations[0][product_colour_sizes_product_id] = 810;
$product_combinations[0][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[0][product_colour_sizes_option_value_size_id] = 904;
$product_combinations[0][quantity] = 999;
$product_combinations[0][product_colour_sizes_name_sizes] = '16-165';
$product_combinations[0][product_colour_sizes_name_colours] = 'Red';
$product_combinations[1][product_colour_sizes_id] = 37104;
$product_combinations[1][product_colour_sizes_product_id] = 810;
$product_combinations[1][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[1][product_colour_sizes_option_value_size_id] = 905;
$product_combinations[1][quantity] = 999;
$product_combinations[1][product_colour_sizes_name_sizes] = '17-175';
$product_combinations[1][product_colour_sizes_name_colours] = 'Red';
$product_combinations[2][product_colour_sizes_id] = 37103;
$product_combinations[2][product_colour_sizes_product_id] = 810;
$product_combinations[2][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[2][product_colour_sizes_option_value_size_id] = 906;
$product_combinations[2][quantity] = 999;
$product_combinations[2][product_colour_sizes_name_sizes] = '185-19';
$product_combinations[2][product_colour_sizes_name_colours] = 'Red';
$product_combinations[3][product_colour_sizes_id] = 37102;
$product_combinations[3][product_colour_sizes_product_id] = 810;
$product_combinations[3][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[3][product_colour_sizes_option_value_size_id] = 907;
$product_combinations[3][quantity] = 999;
$product_combinations[3][product_colour_sizes_name_sizes] = '19.5';
$product_combinations[3][product_colour_sizes_name_colours] = 'Red';
$product_combinations[4][product_colour_sizes_id] = 37102;
$product_combinations[4][product_colour_sizes_product_id] = 810;
$product_combinations[4][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[4][product_colour_sizes_option_value_size_id] = 907;
$product_combinations[4][quantity] = 999;
$product_combinations[4][product_colour_sizes_name_sizes] = '19.5';
$product_combinations[4][product_colour_sizes_name_colours] = 'Blue';
$product_combinations[5][product_colour_sizes_id] = 37103;
$product_combinations[5][product_colour_sizes_product_id] = 810;
$product_combinations[5][product_colour_sizes_option_value_colour_id] = 61;
$product_combinations[5][product_colour_sizes_option_value_size_id] = 906;
$product_combinations[5][quantity] = 999;
$product_combinations[5][product_colour_sizes_name_sizes] = '185-19';
$product_combinations[5][product_colour_sizes_name_colours] = 'Blue';
And here is the reworked code logic based on the original poster’s code & desired output:
// Init the colours array.
$colours = array();
// Init the sizes array.
$sizes = array();
// Init the increment array.
$increment = array();
// Main logic.
foreach ($product_combinations as $key => $product_combination){
// Set the color & id.
$color = $product_combinations[$key]['product_colour_sizes_name_colours'];
$id = $product_combinations[$key]['product_colour_sizes_option_value_size_id'];
// Set the sizes array data.
$sizes[$color][$id] = $product_combinations[$key]['product_colour_sizes_name_sizes'];
$sizes[$color]['quantity'] = $product_combinations[$key]['quantity'];
$sizes[$color]['product_colour_sizes_id'] = $product_combination['product_colour_sizes_id'];
$sizes[$color]['product_colour_sizes_product_id'] = $product_combination['product_colour_sizes_product_id'];
$sizes[$color]['product_colour_sizes_option_value_colour_id'] = $product_combination['product_colour_sizes_option_value_colour_id'];
$sizes[$color]['product_colour_sizes_option_value_size_id' . $increment[$color]] = $product_combination['product_colour_sizes_option_value_size_id'];
$increment[$color]++;
// Now place the colours array in the sizes array.
$colours[$color] = $sizes[$color];
} // foreach
And the output of that would be:
Array
(
[Red] => Array
(
[904] => 16-165
[quantity] => 999
[product_colour_sizes_id] => 37102
[product_colour_sizes_product_id] => 810
[product_colour_sizes_option_value_colour_id] => 61
[product_colour_sizes_option_value_size_id] => 904
[905] => 17-175
[product_colour_sizes_option_value_size_id1] => 905
[906] => 185-19
[product_colour_sizes_option_value_size_id2] => 906
[907] => 19.5
[product_colour_sizes_option_value_size_id3] => 907
)
[Blue] => Array
(
[907] => 19.5
[quantity] => 999
[product_colour_sizes_id] => 37103
[product_colour_sizes_product_id] => 810
[product_colour_sizes_option_value_colour_id] => 61
[product_colour_sizes_option_value_size_id] => 907
[906] => 185-19
[product_colour_sizes_option_value_size_id1] => 906
)
)

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.

Modify key and value inside multidimensional array PHP

I'm trying to modify a key and value from a multidimensional array with PHP
[0] => Array (
[price] => 1-0
[shipping] => 0-1
[description] => 0-1
[meta_title] => 0-1
)
[1] => Array (
[price] => 1-0
[shipping] => 0-1
[meta_title] => 0-1
)
Let's say I wanna modify description value and key name in 0, I wanna do with index numbers for some reasons, my code so far and where I'm stucked:
$pageID = 0;
$text_id = 2;
$i=0;
foreach($oJson[$pageID] as $t => $f){
if($i==$text_id){
}
$i++;
}
Desired result:
[0] => Array (
[price] => 1-0
[shipping] => 0-1
[modified_description] => 2-3
[meta_title] => 0-1
)
[1] => Array (
[price] => 1-0
[shipping] => 0-1
[meta_title] => 0-1
)
Thanks a lot for your precious help.
Since you want to keep the associative key order, you can use array_keys, array_values, and array_combine.
$pageID = 0;
$text_id = 2;
$newKeys = array_keys($oJson[$pageID]);
$newValues = array_values($oJson[$pageID]);
$newKeys[$text_id] = 'modified_' . $newKeys[$text_id];
$newValues[$text_id] = '2-3';
$oJson[$pageID] = array_combine($newKeys, $newValues);
Try this:
$oJson = array('0' => Array (
'price' => '1-0',
'shipping' => '0-1',
'description' =>'0-1',
'meta_title' => '0-1',
),
'1' => Array (
'price' => '1-0',
'shipping' => '0-1',
'meta_title' => '0-1',
)
);
$pageID = 0;
$text_id = 2;
foreach($oJson as $key => $subArray)
{
if($key == $pageID)
{
$tempArray = array();
for($i = 0; $i < count($subArray); $i++)
{
if($i == $text_id)
{
$tempArray = array_slice($subArray, 0, $text_id);
$tempArray['modified_description'] = '2-3';
$tempArray = array_merge($tempArray, array_slice($subArray, $text_id + 1));
}
}
if(!empty($tempArray))
$oJson[$key] = $tempArray;
}
}
print_r($oJson);
Working example: CLICK!

How to remove duplicate entries from associative array in php

My array is
Array
(
[0] => Array
(
[id] => 20
[new_id] => 958
[affiliate_id] => 33
)
[1] => Array
(
[id] => 21
[new_id] => 959
[affiliate_id] => 45
)
[2] => Array
(
[id] => 22
[new_id] => 960
[affiliate_id] => 23
)
[3] => Array
(
[id] => 23
[new_id] => 961
[affiliate_id] => 33
)
)
and i want array
Array
(
[0] => Array
(
[id] => 20
[new_id] => 958
[affiliate_id] => 33
)
[1] => Array
(
[id] => 21
[new_id] => 959
[affiliate_id] => 45
)
[2] => Array
(
[id] => 22
[new_id] => 960
[affiliate_id] => 23
)
)
I want to remove duplicates value of affiliate_id . According to first array i am getting affiliate_id's value is 33 for two time. But i want it for one time. So in my second array (which will be my answer) i remove it.
Try something like this, not so pretty as array_ one liners, but still:
$existing_aff_ids = array();
$unique = array();
foreach ($affiliate as $aff) {
if (!isset($existing_aff_ids[$aff['affiliate_id']])) {
$unique[] = $aff;
$existing_aff_ids[$aff['affiliate_id']] = 1;
}
}
Given $affiliates as in your answer, looping over the array and checking for affiliate_id would do the trick
$unique_affiliates = array();
foreach($affiliates as $affiliate) {
$affiliate_key = $affiliate['key'];
/* Variant 1 */
$unique_affiliates[$affiliate_key] = $affiliate;
/* Variant 2 */
if(!isset($unique_affiliates[$affiliate_key])) {
$unique_affiliates[$affiliate_key] = $affiliate;
}
}
All entries in $unique_affiliates will have unique affiliate_keys. Variant 1 will contain the last occurrence of each afffiliate_key (as in your example), whereas variant 2 will add the first occurrence of any affiliate_key and just ignore all subsequent ones.
These are not duplicate values :
1. $input = array_map("unserialize",
array_unique(array_map("serialize", $data))
2. array_values(array_unique($data))
Both this case fails because of the unique id values are there it requires all values to be same to consider it as duplicate.
Solution:Will making the array check the value of the corresponding field.
foreach($data as $key=>$val)
{
if (is_array($val))
{
$val2 = arrayUnique($val);
}
else
{
$val2 = $val;
$newArray=array_unique($data);
$newArray=deleteEmpty($newArray);
break;
}
if (!empty($val2))
{
$newArray[$key] = $val2;
}
}
print_r($newArray);

Sorting sub array values in MultiDimension Array

I am trying to sort an array sent from an XML feed.
The Array looks like this from print_r($answer);:
Array
(
[size] => Array
(
[0] => 1.5m x 1.5m
[1] => 1.5m x 3m
[2] => 3m x 6.0m
[3] => 3m x 2.3m
)
[rate] => Array
(
[0] => 80
[1] => 135
[2] => 295
[3] => 180
)
[sortorder] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 2
)
.
.
.
)
I want to get out the array:
Array
(
[size] => Array
(
[0] => 3m x 6.0
[1] => 3m x 2.3m
[2] => 1.5m x 1.5m
[3] => 1.5m x 3m
)
[rate] => Array
(
[0] => 295
[1] => 180
[2] => 80
[3] => 135
)
[sortorder] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
.
.
.
)
What I am trying to do is use the Sort Order sub array to display the items in that order
I have tried a number of uasort() and array_multisort() examples, but all seem to order the sub arrays and not the values inside the sub arrays
Any ideas will be a great help. Cheers
This one sorts $answer['sortorder'] and uses those keys to sort the rest of $answer without restructuring first.
// sort $answer['sortorder'] and retrieve indices.
asort($answer['sortorder']);
$idx = array_keys($answer['sortorder']);
// do sorting
$sorted = array();
foreach($answer as $key=>$subarr) {
if ($key != 'sortorder') { // don't sort
foreach($idx as $i) {
$sorted[$key][] = $subarr[$i];
}
} else {
// $answer['sortorder'] is already sorted.
$sorted[$key] = $subarr;
}
}
print_r($sorted);
See it in action here.
This approach will re-structure the array with the sort order being the index, sort the array, then return it to it's original structure.
echo '<pre>';
$array['size'][0] = '1.5m x 1.5m';
$array['size'][1] = '1.5m x 3m';
$array['size'][2] = '3m x 6.0m';
$array['size'][3] = '3m x 2.3m';
$array['rate'][0] = 80;
$array['rate'][1] = 135;
$array['rate'][2] = 295;
$array['rate'][3] = 180;
$array['sortorder'][0] = 3;
$array['sortorder'][1] = 4;
$array['sortorder'][2] = 1;
$array['sortorder'][3] = 2;
$temp = array();
foreach($array['sortorder'] as $key => $value)
{
$temp[$array['sortorder'][$key]] = array(
'size'=>$array['size'][$key],
'rate'=>$array['rate'][$key],
'sortorder'=>$array['sortorder'][$key]
);
}
ksort($temp);
$array = array();
foreach($temp as $key => $value)
{
$array['size'][] = $value['size'];
$array['rate'][] = $value['rate'];
$array['sortorder'][] = $value['sortorder'];
}
print_r($array);
May I propose to use a different array structure that bundles each size, rate and sort order into one item:
array(
array('size' => '...', 'rate' => '...', 'sort order' => '...'),
...
)
That makes it trivial to sort, and in fact easier to work with in general.
This PHP 5.3+ code does this transformation and sorting:
$answer = array_map(function ($size, $rate, $sortorder) {
return compact('size', 'rate', 'sortorder');
}, $answer);
usort($answer, function ($a, $b) { return $a['sortorder'] - $b['sortorder']; });

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);

Categories