Sort php array by multipule values - php

I'm trying to sort this array by the date but the array is merged and has two different types of date.
[0] => Object
[Something] => hey
[date]=>2010-01-03
[1] => Object
[something] => heyagain
[somethingelse] => heythere
[posted_date] => 2011-08-22
I want this array to sort the whole array by date and posted date but the array comes out in order as:
Array1=>(date1,date2,date3) Array2=>(date1,date2,date3)
For instance in (Array2,date2) may be before (Array1,date1) but it does not sort that way. I want to see
Output=>Array2(date1),Array2(date2),Array1(date1),Array2(date3),Array1(date2),Array1(date3
I have tried array_multisort($merge, SORT_NUMERIC, $arg, 'posted_date', SORT_DESC, 'date', SORT_DESC) and a few other but I can't get it to work. I hope this isn't confusing anyone.

To use array_multisort, you need to create multiple arrays first, e.g. accessing properties that you want to sort over and wrapping those into another array.
The manual page gives good examples how to do this or that. However strings just won't work:
array_multisort($merge, SORT_NUMERIC, $arg, 'posted_date', SORT_DESC, 'date', SORT_DESC);
See as well: Sort data of Php array by values of another array

Related

sorting array based on child array[0] (unix) value

I need an array sorted by Unix timestamp values. I attempted to use both ksort and krsort before realising that occasionally the timestamp values might be the same (and you cannot have duplicate keys in arrays).
Here's an example array I may be faced with:
$array = array(
[
"unix" => 1556547761, // notice the two duplicate unix values
"random" => 4
],
[
"unix" => 1556547761,
"random" => 2
],
[
"unix" => 1556547769,
"random" => 5
],
[
"unix" => 1556547765, // this should be in the 3rd position
"random" => 9
]
);
So what I'm trying to do is sort them all based on each child arrays unix value, however I cannot figure out how to do so. I have tried countless insane ways (including all other sort functions and many, many for loops) to figure it out - but to no avail.
All help is appreciated.
You can use usort which sort your array by given function
Define function as:
function cmpByUnix($a, $b) {
return $a["unix"] - $b["unix"];
}
And use with: usort($array, "cmpByUnix");
Live example: 3v4l
Notice you can also use asort($array); but this will compare also the "random" field and keep the key - if this what you need then look at Mangesh answer
array_multisort() — Sort multiple or multi-dimensional arrays
array_columns() — Return the values from a single column in the input array
You can use array_multisort() and array_column(), then provide your desired sort order (SORT_ASC or SORT_DESC).
array_multisort(array_column($array, "unix"), SORT_ASC, $array);
Explanation:
In array_multisort(), arrays are sorted by the first array given. You can see we are using array_column($array, "unix"), which means that the second parameter is the order of sorting (ascending or descending) and the third parameter is the original array.
This is the result of array_column($array, "unix"):
Array(
[0] => 1556547761
[1] => 1556547761
[2] => 1556547765
[3] => 1556547769
)
This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.
Note:If two members compare as equal, their relative order in the sorted array is undefined.
Refer : https://www.php.net/manual/en/function.asort.php
asort($array);
echo "<pre>";
print_r($array);
echo "</pre>";
It will give you the output as
Array
(
[1] => Array
(
[unix] => 1556547761
[random] => 2
)
[0] => Array
(
[unix] => 1556547761
[random] => 4
)
[3] => Array
(
[unix] => 1556547765
[random] => 9
)
[2] => Array
(
[unix] => 1556547769
[random] => 5
)
)
You can keep the array key [1],[0],[3],[2]) as it is Or you can keep it as sequential as per your requirement.

Unable to convert array according to date time value in php

I have a array named $itemIds and it consists of the following datas. The key is the items id and the value is the date time that can be converted to a readable date.What I want to do is simply sort the order of the array according to the value (time). I'm totally new to php and some examples or tips would be great ! I would love to hear from you.
Array
(
[10477] => 1508898726
[10549] => 1508898744
[10891] => 1508898752
)
If I use this code I get the following data from the print_r.
if (isset($itemIds)) {
$time = array();
foreach ($itemIds as $key => $val) {
print_r($key.'=>'.date('m/d/Y H:i:s', $val));
}
}
The problem starts from here.I want to sort(asc & desc) the $itemsIds according to the date time.
10477 => 10/25/2017 11:32:06
10549 => 10/25/2017 11:32:24
10891 => 10/25/2017 11:32:32
I want to sort the data then use array_keys($shopIds) to change it like the following data
Array
(
[0] => 10477
[1] => 10549
[2] => 10891
)
I think the easier approach would be to sort the data and then flip it so the keys are values. If you look at your data you are initially dealing with actual timestamps from what I can see. Use them, don't change them to strings. I say this because you are just causing yourself to do extra work without any reason and introducing a layer of complexity that is not needed. Instead I would do the following:
$array = [
10477 => 1508898726,
10549 => 1508898744,
10891 => 1508898752,
];
arsort($array);
$sorted_array = array_values(array_flip($array));
This is easy to read and does not involve the extra function. The result you are left with is:
Array
(
[0] => 10477
[1] => 10549
[2] => 10891
)
A little explanation:
I am using arsort() or asort() (based on the direction you want to sort) in order to sort based on the values in the array.
Then I use array_flip() on the array in order to swap the keys and values.
And last I am using array_keys() to reset the indexes in the array and maintain its sort.
Hope this helps!

How do I reorganize an array in PHP?

I am trying to figure out how to reorganize an array..
I have a multidimensional array(Ill call that original_array) and I would like to take the first array within original_array and set the values as keys in a new array. I also want to take the values of the second array in original_array and make them keys and then set the values of the third array in original_array as the values for those keys.
Here is an example of original_array:
Array (
[id] => Array (
[0] => 1
[1] => 3
)
[reward] => Array (
[0] => Movie
[1] => Trip
)
[cost] => Array (
[0] => 50
[1] => 200
)
)
Basically what I would like to do is look like this:
Array (
[1] => Array (
[Movie] => 50
)
[3] => Array (
[Trip] => 200
)
)
Is there a simple and elegant way to merge these like this?
I have spent hours trying to figure this out using array_merge, array_merge_recursive.. etc. And have search SO far and wide for a similar questions, but I haven't found anything that does what I am after.
I was able to correctly combine the 2nd and 3rd arrays in original_array with array_combine. But, I am at a loss as how to combine that result with the 1st array's values in original_array.
Thanks in advance to any help!
Well, the dirty way would be just use combine array functions like array_combine with the input:
$new_array = array_combine(
$array['id'], // parent keys
// combine chunked combined sub keys :p
array_chunk(array_combine($array['reward'], $array['cost']), 1, true)
);
There may be some incantation of array_*() merging functions that could produce what you're looking for, but it is far easier to just iterate over the original array's [id] sub-array and use its values to create new sub-array keys in a different output array.
// To hold your output
$output = array();
// Iterate the original array's [id] sub-array
foreach ($original['id'] as $idxkey => $newkey) {
// Add a sub-array using $newkey to the output array
$output[$newkey] = array(
// Using the index (not value), retrieve the corresponding reward
// value to use as the new array key
// and corresponding cost to use as the new subarray value
$original['reward'][$idxkey] => $original['cost'][$idxkey]
);
}
Here is a demonstration: https://3v4l.org/2pac3
This should work for you:
First you can get the keys for the main array into a separate variable with array_shift(), which will just remove the first element from your array, which is the array holding the keys.
Then use array_map() to loop through both of your subArrays and use reward as key with the cost values as value and return it in an array. At the end you just have to array_combine() your keys $keys with the new created array.
Code:
<?php
$keys = array_shift($arr);
$result = array_combine($keys, array_map(function($k, $v){
return [$k => $v];
}, $arr["reward"], $arr["cost"]));
print_r($result);
?>
You might wanna take a look at BaseArrayHelper from Yii 2.0 Framework.
Although this file is part of a framework it has only very few dependencies and you should be able to use just this file or parts of it in your code with small modifications.
An example for your use case can be found in the index() method.

get difference from three arrays by value

I am trying to get the difference between three arrays
array_diff() function doesn't help because it matches only the first array with others
For example I want to compare three arrays and each array has one of the element different from each other like this
$a1=array("a"=>"red","b"=>"green","c"=>"blue", 'v1' => 'sss');
$a2=array("e"=>"red","f"=>"green","g"=>"blue", 'v2' => 'ss');
$a3=array("e"=>"red","f"=>"green","g"=>"blue", 'v3' => 's');
when I use array_diff() on these arrays it would just show me the unique value out of other two arrays
$res = array_diff($a1, $a2, $a3);
print_r($res);
It's result would be Array ( [v1] => sss )
While I want it to tell me the unique values in all these arrays like this Array ( [v1] => sss [v2] => ss [v3] => s )
I tried other array comparison functions but couldn't find one to compare all given array to each other instead of comparing just one array to others
$var=(array_diff($a1,$a2,$a3));
$var1=(array_diff($a2,$a1,$a3));
$var2=(array_diff($a3,$a2,$a1));
$v=array_merge($var,$var1,$var2);
I guess this helps for you.check it out..

Finding and marking the largest of three values in a two dimensional array

I am working on a display screen for our office, and I can't seem to think of a good way to find the largest numerical value in a set of data in a two dimensional array. I've looked at using max() and also asort() but they don't seem to cope with a two dimensional array.
I'm returning my data through our mysql class, so the rows are returned in a two dimensional array.
Array(
[0] => Array(
[am] => 12,
[sales] => 981),
[1] => Array(
[am] => 43,
[sales] => 1012),
[2] => Array(
[am] => 17,
[sales] => 876)
)
I need to output a class when foreaching the data in my table for the AM with the highest sales value. Short of comparing them all in > if statements. I have tried to get max() on the array, but it returns an array, as it's look within the dimension. When pointing it at a specific dimension it returns the key not the value.
I figured that I could asort() the array and pop the top value off, store it in a variable and then compare against that in my foreach() loop, but that seems to have trouble sorting across two dimensions.
Lastly, I figured that I could foreach() the values, comparing them against the previous one each time, untill I found the largest. This approach however means storing every value, luckily only three, but then comparing against them all again.
Surely there must be a simpler way to achieve this, short of converting it into a single dimension array, then doing an asort() on that?
<?php
function customSort($a, $b)
{
if($a['sales'] == $b['sales'])
return 0;
else
return $a['sales'] < $b['sales'] ? -1 : 1;
}
usort($array, 'customSort');
?>
Is what I would do

Categories