Sort multiple array with same order - php

Below is example of my array
["value"]=>
array(16) {
[0]=>
string(5) "4"
[1]=>
string(4) "2"
[2]=>
string(4) "1"
[3]=>
string(4) "3"
}
["id"]=>
array(16) {
[0]=>
string(4) "four-id"
[1]=>
string(4) "two-id"
[2]=>
string(4) "one-id"
[3]=>
string(4) "three-id"
}
Now I am sorrting using array_multisort($arr["value"],SORT_NUMERIC, SORT_DESC); , which results in below output.
["value"]=>
array(16) {
[0]=>
string(5) "4"
[1]=>
string(4) "3"
[2]=>
string(4) "2"
[3]=>
string(4) "1"
}
["id"]=>
array(16) {
[0]=>
string(4) "four-id"
[1]=>
string(4) "two-id"
[2]=>
string(4) "one-id"
[3]=>
string(4) "three-id"
}
I want $arr["id"] to be sorted based on same sorting order of $arr["value"] like below
["id"]=>
array(16) {
[0]=>
string(4) "four-id"
[1]=>
string(4) "three-id"
[2]=>
string(4) "two-id"
[3]=>
string(4) "one-id"
}

You can use krsort
$array_1 = Array(4,2,1,3);
$array_2 = Array('four-id','two-id','one-id','three-id');
foreach ($array_1 as $key => $value) {
$array_merged[$value] = $array_2[$key];
}
krsort($array_merged);
foreach ($array_merged as $key => $value) {
$new_array_1[] = $key;
$new_array_2[] = $value;
}
echo '<pre>'.print_r( $new_array_1, true ).print_r( $new_array_2, true ).'</pre>';

Related

Sort multidimensional array by specific key value

I'm working on algorithm to display my events on the website.
I want to sort my multidimensional array by specific key value.
My array:
["2022-02-28"]=>
array(1) {
[0]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
["2022-03-01"]=>
array(2) {
[2]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
[3]=>
array(3) {
["post_id"]=>
string(4) "3784"
["time"]=>
string(5) "13:00"
["priority"]=>
string(1) "0"
}
}
["2022-03-03"]=>
array(1) {
[5]=>
array(3) {
["post_id"]=>
string(4) "3663"
["time"]=>
string(5) "13:06"
["priority"]=>
string(1) "1"
}
}
}
I want to sort the array by "time" key value. So for example at this index :
["2022-03-01"]=>
array(2) {
[2]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
[3]=>
array(3) {
["post_id"]=>
string(4) "3784"
["time"]=>
string(5) "13:00"
["priority"]=>
string(1) "0"
}
}
I want first 13:00 to appear then 16:05. Thank you for your help in advance! :)
Try with this:
<?php
$arr = array();
$arr["2022-02-28"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"4856", "time"=>"13:05", "priority"=>"3")];
$arr["2022-03-01"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"3636", "time"=>"13:05", "priority"=>"1")
];
foreach($arr as $key => $value){
usort($value, function($a,$b){
return strtotime($a["time"])>strtotime($b["time"]);
});
$arr[$key] = $value;
}
echo "<pre>";
var_dump($arr);
echo "</pre>";
Output:
array(2) {
["2022-02-28"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "4856"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "3"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
["2022-03-01"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "3636"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "1"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
Use usort for define custom sort.
function time_sort(array $arr){
usort($arr, function($a, $b){
return strcmp($a['time'], $b['time']);
});
}

How to split multidimensional array into arrays based on the values - PHP

I have this array, it could look something like this:
array(756) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[2]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[3]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[4]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[5]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
[6]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[7]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[8]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[9]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[10]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[11]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
etc...};
How would i go about looping thru and splitting it up into arrays based on the value in the inner arrays[0] ex: "joint_temps5".
I have tested quite a few things but without success. My problem mainly is i dont know what might be in the string in the arrays.
I would like to end up with arrays like:
$array1[] = array(x_amount){
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
}
$array2[] = array(x_amount){
[0]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
}
}
etc.
I would recommend to create a new array from your input array, using the value as an index of the array to be created, like so:
// test-set: input array is $a
$a[0] = array("joint_temps5","23.5");
$a[1] = array("joint_temps3","24");
$a[2] = array("joint_temps2","24.5");
$a[3] = array("joint_temps1","25");
$a[4] = array("joint_temps0","25.5");
$a[5] = array("joint_temps5","23.5");
$a[6] = array("joint_temps4","23.5");
$a[7] = array("joint_temps3","24");
$a[8] = array("joint_temps2","24.5");
$a[9] = array("joint_temps1","25");
foreach($a as $key => $value){
$b[$value[0]][] = $value; // *Explained below
}
*"Explained below": $a is the source array, $b is the newly created array.
$b[$value[0]][] means it wil create a new element for array $b[$value[0]]. And $value[0] will be substituted by the first value in the element of $a that the foreach loop hits.
Example: the first element of $a is this array: array("joint_temps5","23.5"). So in the foreach loop, the text "joint_temps5" ($value[0] in the foreach) will be used as a key/index to create a new element for array $b. The [] means that with every new execution of this line, a new element, with that key value $value[0], will be added.
It will result in:
array(6) {
["joint_temps5"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
}
["joint_temps3"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
}
["joint_temps2"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
}
["joint_temps1"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
}
["joint_temps0"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
}
["joint_temps4"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
}
}
You could loop through your array, and populate a new array using the string as a key, so something like:
foreach ($array as $working_array) {
$new_array[$working_array[0]][] = $working_array[1]; }
Which would give you an array something like :
$new_array["joint_temps5"]=> array(2) {
[0]=> "23.5"
[1]=> "23.5"}
If you needed to you could then parse that into an array in the format you desire quite easily.

PHP push new data into given array

I may seem having difficulty in understanding on how array works. So result, I can't work on the issue.
foreach($results as $key => $value){
$product_key = array(
'key' => $key
);
array_push($results, $product_key);
}
var_dump($results); exit;
Expected Output
array(2) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
["key"]=>
int(0)
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
["key"]=>
int(1)
}
Unexpected Output
array(4) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
}
[2]=>
array(1) {
["key"]=>
int(0)
}
[3]=>
array(1) {
["key"]=>
int(1)
}
}
You push new value (which is array) to the end of existsing array, what do you expect then?
If you want to modify current interated array value use this approach:
foreach($results as $key => $value) {
// use `->` as `$value` is object
$value->key = $key;
}
var_dump($results); exit;

Merge array where duplicate in multidimensional array

I've been trying to merge this array ONLY WHERE $array[4] exists more than one time, example: $array[4] == 'red' exactly twice. How can I merge only those arrays while keeping the others? I have made several attempts at this and I am willing to include my efforts if asked.
Consider this array:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) "red"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(20) "red"
}
[2]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
TRYING TO ACHIEVE:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) " red"
[5]=>
string(3) "Fee"
[6]=>
string(7) "License"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
foreach ($test as $item) {
if (!isset($merged[$item[4]])) {
// add the item to the merged array using key=$item[4]
$merged[$item[4]] = $item;
} else {
// merge the item with the item that is already in the array at key=$item[4]
$merged[$item[4]] = array_unique(array_merge($merged[$item[4]], $item));
// array_unique is necessary because array_merge will not overwrite numeric keys
}
}
// convert the keys back to numeric (if you care to)
$merged = array_values($merged);

Data mismatch while dumping array in csv file

I have 7 array fetched by mysql and its working correctly each array have exactly 9 rows which are city_names but one of the array has only 7 rows since two values on the results where null . This causes confunsion while dumping file in csv. I use the below code. Is there any way to check it by city name without using foreach for eact array ?
for($i=0 ; $i <= $count-2 ; $i++)
{
$data[] = $all_restaurants_opr_no_temp_off[$i]['city_name'];
$data[] = $all_restaurants_opr[$i]['total'];
$data[] = $all_restaurants_opr_no_temp_off[$i]['total'];
$data[] = $restaurants_opr_temp_off[$i]['total'];
$data[] = $restaurants_opr_operations_closed[$i]['total'];
$data[] = $restaurants_opr_automated[$i]['total'];
$data[] = $restaurants_opr_automated_working[$i]['total'];
$data[] = $restaurants_opr_online_payment[$i]['total'];
$data[] = $restaurants_opr_online_payment_with_tempoff[$i]['total'];
$data[] = $restaurants_opr_atleast_one_order[$i]['total'];
fputcsv($fp, $data,",");
unset($data);
}
Array 1:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "687"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "568"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "139"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "232"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "57"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "113"
}
}
Array 2:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "674"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "547"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "135"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "202"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "56"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "111"
}
}
Array 3:(Problem is here)
array(6) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(2) "13"
}
[1]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(2) "21"
}
[2]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(1) "4"
}
[3]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(2) "30"
}
[4]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(1) "1"
}
[5]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(1) "2"
}
}
I'm not sure I understand the question 100%, but perhaps you can test each value if it is null and put a default value there if it is, for example, for this row, you could use this above your for loop:
foreach($badArray as $b){
$reformattedArray[$b['city_name']] = $b['total'];
}
then replace the bad array line with this in your for loop
data[] = is_null($reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']]) ? 'Default' : $reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']];

Categories