Sort a multidimensional array in PHP - php

I have this multidimensional array, called $rent:
Array
(
[product2] => Array
(
[dates] => Array
(
[2013-07-25] => 2
[2013-07-23] => 1
[2013-07-21] => 3
)
)
[product3] => Array
(
[dates] => Array
(
[2013-07-24] => 5
[2013-07-22] => 4
[2013-07-20] => 3
)
)
[product1] => Array
(
[dates] => Array
(
[2013-07-29] => 1
[2013-07-28] => 2
[2013-07-27] => 2
)
)
)
I'd like to do a double sort:
First, by productX ascending
Then, for each product, by date of rent ascending
So that the resulting array would be:
Array
(
[product1] => Array
(
[dates] => Array
(
[2013-07-27] => 2
[2013-07-28] => 2
[2013-07-29] => 1
)
)
[product2] => Array
(
[dates] => Array
(
[2013-07-21] => 3
[2013-07-23] => 1
[2013-07-25] => 2
)
)
[product3] => Array
(
[dates] => Array
(
[2013-07-20] => 3
[2013-07-22] => 4
[2013-07-24] => 5
)
)
)
How can I reach this? Many thanks in advance

try this:
ksort($rent);
foreach($rent as &$item) {
ksort($item['dates']);
}

You can simply ksort the products, then iterate through them and use the same for the dates key.
ksort($products);
foreach($products as &$product)
ksort($product['dates']);
Where $products is the array you showed us. Note that you need to pass the value in the foreach loop as a reference (using the & operator) otherwise the changes won't be updated in the original array.

for my understanding of your problem; Nadh solution is almost there. but i believe you want ksort()
this is my corrections to Nadh answer
ksort($rent);
foreach($rent as $product => $dates) {
ksort($rent[$product]['dates']);
}
print_r($rent);

Related

How to get common values from two different arrays in the PHP Language

I have two different arrays with the same delivery_type_id
Array 1:
Array
(
[0] => Array
(
[delivery_type_id] => 2
[delivery_code] => InStorePickup
[delivery_title] => In Store Pickup
)
[1] => Array
(
[delivery_type_id] => 3
[delivery_code] => On-FarmPickup
[delivery_title] => On-Farm Pickup
)
)
Array 2:
Array
(
[0] => Array
(
[delivery_type_id] => 2
)
)
I need the common delivery_type_id from the array as follow
Result:
Array
(
[0] => Array
(
[delivery_type_id] => 2
[delivery_code] => InStorePickup
[delivery_title] => In Store Pickup
)
)
I have tried array_intersect() with array_column but, it's not working. Thanks in advance.
Loop through them and compare.
foreach($array0 as $a){
foreach($array1 as $b){
if($a['delivery_type_id']==$b['delivery_type_id']){ do things }
}
}

How to Extract Values from php array

I'm trying to return the LARGEST numerical value associated with the 'reduced' array below. Easy enough if there is one value but many have two- as below. I'm using
$reduced_array = $data['rates'][1]['rates'][0];
but this only works in returning the first value. I need to return just the highest value however- so below it would be 8.
would something like
if(count($data['rates'][1]['rates']) > 2) {
***return largest value here***
work? I'm just not sure how to perform the asterisked task- maybe a for loop? here is the array.
Array
(
[rates] => Array
(
[0] => Array
(
[name] => Super Reduced
[rates] => Array
(
)
)
[1] => Array
(
[name] => Reduced
[rates] => Array
(
[0] => 5
[1] => 8
)
)
[2] => Array
(
[name] => Standard
[rates] => Array
(
[0] => 23
)
)
[3] => Array
(
[name] => Increased
[rates] => Array
(
)
)
[4] => Array
(
[name] => Parking
[rates] => Array
(
)
)
)
[disclaimer] => Rates data is based on information published by the European Commission, updated 1st January 2017.
)
Thanks for any help
You can use max() for that.
$array = array(
"rates" => array(
array(
"name" => "Super Reduced",
"rates" => array()
),
array(
"name" => "Reduced",
"rates" => array(
5,
8
)
)
)
);
echo max($array["rates"][1]["rates"]); // 8
Easiest solution will be using of max on inner array
example:
if(!empty($data['rates'][1]['rates'])) {
$maxvalue = max($data['rates'][1]['rates']);
}

Merge array into multidimensional array

How can I merge two arrays in the way I explain below?
Using print_r(), this is the output of the two arrays;
The first one;
Array
(
[created] => 1
[approved] => 1
)
And the second array;
Array
(
[created] => Array
(
[label] => Order created
[roles] => Array
(
[0] => ROLE_USER
)
[next_states] => Array
(
[approve] => Array
(
[target] => approved
)
)
)
[approved] => Array
(
[label] => Order approved
[roles] => Array
(
[0] => ROLE_USER
)
[next_states] => Array
(
[order] => Array
(
[target] => ordered
)
)
)
)
How can I merge the arrays so that the $value (which will be either true or false) from the first array can be merged to the second one as a [state] as follows (There are comments at the lines [state]);
Array
(
[created] => Array
(
[label] => Order created
[roles] => Array
(
[0] => ROLE_USER
)
[next_states] => Array
(
[approve] => Array
(
[target] => approved
)
)
[state] => 1 // I want to add this line here from the other array
)
[approved] => Array
(
[label] => Order approved
[roles] => Array
(
[0] => ROLE_USER
)
[next_states] => Array
(
[order] => Array
(
[target] => ordered
)
)
[state] => 1 // I want to add this line here from the other array
)
)
Note that the $keys from the first array and the second array are the same, but I want to add the $values to the arrays that serves as $values in the second array. What will be the best way to do this?
foreach($array1 as $a => $b)
$array2[$a]['state'] = $b;
edit:
I've seen the comment. To set state to false when the right key doesn't exist in the first array, you can use the following code.
foreach($array2 as $a => &$b)
$b['state'] = isset($array1[$a]) && $array1[$a];
You can use array_merge_recursive function. It used merge the array dynamically
DEMO Please click here
Another example

merging of 2 identical keys in one array

Need help from the team,
I have this scenario of having 2 identical keys in each array with different values, i want them to be merged into one key were the values also are in it
example:
arrayData1(
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
)
)
arrayData2(
[2] => Array
(
[EXP3] => Array (records...)
[EXP4] => Array (records...)
)
)
Having the output like this:
arrayFinal (
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
[EXP3] => Array (records...)
[EXP3] => Array (records...)
)
)
Thanks!
First of all you cannot have two same keys in a single array, what you can do is use the array_merge_recursive function in php to merge both the arrays, and the repeating keys will have a new array with all the repeating key values..
$array1 = [
'EXP1' => [1,2,3],
'EXP2' => [2,3,4]
];
$array2 = [
'EXP2' => [5,6,7],
'EXP3' => [8,9,10]
];
Now there are two EXP2 keys, so when you use array_merge_recursive() you get something like this,
print_r(array_merge_recursive($array1, $array2));
//output Array (
[EXP1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[EXP2] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
)
[EXP3] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)

Multidimensional array sort on multiple keys

Hi I've been using the array_sort() function found here for some time to sort results from multiple APIs but now I have the need to sort by two keys simultaneously.
The two keys I need to sort on are deal_score DESC and date_start DESC
The properties of this array are as follows.
Record 2 has the highest deal_score so should come first
Records 0 and 1 have the same deal_score but date_start is higher on record 1 so the final order of results should be 2, 1, 0
Here's an example array which has been trimmed down for readability.
[0] => Array
(
[db_id] => 414314
[date_start] => 2012-04-17
[deal_score] => 81.3
[deal_statements] => Array
(
[0] => 49.85
[1] => 2.11
)
)
[1] => Array
(
[db_id] => 414409
[date_start] => 2012-04-20
[deal_score] => 81.3
[deal_statements] => Array
(
[0] => 28.2
[1] => 21.41
)
)
[2] => Array
(
[db_id] => 1345923
[date_start] => 2012-04-17
[deal_score] => 85
[deal_statements] => Array
(
[0] => 18.1
[1] => 22.16
)
)
Any help on this will be greatly appreciated.
sth. like this should do:
foreach ($data as $key => $row) {
$score[$key] = $row['deal_score'];
$dates[$key] = $row['date_start'];
}
array_multisort($score, SORT_ASC, $dates, SORT_ASC, $data);
3rd. example on http://php.net/manual/en/function.array-multisort.php pretty much explains it.
Cheers.

Categories