This question already has answers here:
PHP sort array by two field values [duplicate]
(6 answers)
Closed 5 years ago.
Array
(
[0] => Array
(
[sales] => 117513.00000000
[month] => 1
[month_name] => January
[year] => 2018
)
[1] => Array
(
[purchases] => 136350.00000000
[month] => 9
[month_name] => September
[year] => 2017
)
[2] => Array
(
[sales] => 23025.00000000
[month] => 11
[month_name] => November
[year] => 2017
)
[3] => Array
(
[sales] => 6447.00000000
[month] => 12
[month_name] => December
[year] => 2017
)
)
Here I already sorted the array by year wise , but I need to sort by month descending .
Array
(
[0] => Array
(
[sales] => 117513.00000000
[month] => 1
[month_name] => January
[year] => 2018
)
[1] => Array
(
[purchases] => 6447.00000000
[month] => 12
[month_name] => December
[year] => 2017
)
[2] => Array
(
[sales] => 23025.00000000
[month] => 11
[month_name] => November
[year] => 2017
)
[3] => Array
(
[sales] => 136350.00000000
[month] => 9
[month_name] => September
[year] => 2017
)
)
How can I get the second format , which is sorted by month..
Use array_multisort function to do this.
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$month[$key] = $row['month'];
}
array_multisort($month, SORT_DESC);
?>
Related
well this must a easy one but can't seem to figure it out I have this field range in an array that prints an id which repeats it self on array and would like to convert it to a sequence count starting from 0.
Live example: https://3v4l.org/Sf4nI#v7.0.33
Current output:
Array
(
[0] => Array
(
[range] => 336
[year] => 2020
[month] => 222
)
[1] => Array
(
[range] => 336
[year] => 2020
[month] => 222
)
[2] => Array
(
[range] => 390
[year] => 2020
[month] => 222
)
[3] => Array
(
[range] => 390
[year] => 2021
[month] => 222
)
)
Output I need:
Array
(
[0] => Array
(
[range] => 0
[year] => 2020
[month] => 222
)
[1] => Array
(
[range] => 0
[year] => 2020
[month] => 222
)
[2] => Array
(
[range] => 1
[year] => 2020
[month] => 222
)
[3] => Array
(
[range] => 1
[year] => 2021
[month] => 222
)
)
I think the simple solution here is to extract all range values from the array and remove the duplicated values, to make it start from 0 we will use array_values and flip it to can get the id by range value
$idList = array_flip(array_values(array_unique(array_column($array, 'range'))));
now you can get the id for any range you want like this
$idList[336];
// or
$idList[390];
https://3v4l.org/kUa8D#v7.0.33
hope it's helpful
After generating group array from an associative array, now the array is in the following format. And to make this group array I have used the following code:
foreach($upcoming_data_arr as $key => $item)
{
$arr[$item['year']][$key] = $item;
}
Array
(
[2018] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
[2] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[3] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
)
Now I need another group array according to month within year group. And in this case group data array will be like following:
Array
(
[2018] => Array
(
[11] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[2] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
[12] => Array
(
[0] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
)
)
)
How we will get this output?
Add one more "dimension" when you are creating an array identified by $item['month'] and let php decide last "dimension" key by []:
foreach($upcoming_data_arr as $key => $item) {
$arr[$item['year']][$item['month']][] = $item;
}
I have pulled out data from my database of eps of companies for last 2 years I wanted to sum up eps by using a small formula what happened here is I pulled out data in the form of array now I want to put formula like this `
`
$curr_eps - $old_eps / $old_eps * 100;
With the out put I am getting I am unable to put formula for every company and get separate calculated values
My output data is like this
foreach($data3 as $key => $pr_data) {
$prof_data[] = $pr_data;
}
Array
(
[0] => Array
(
[eps] => -0.28
[year] => 2015
)
[1] => Array
(
[eps] => 3.33
[year] => 2014
)
[2] => Array
(
[eps] => 0.90
[year] => 2015
)
[3] => Array
(
[eps] => 0.81
[year] => 2014
)
[4] => Array
(
[eps] => 1.05
[year] => 2016
)
[5] => Array
(
[eps] => 3.71
[year] => 2015
)
[6] => Array
(
[eps] => 1.61
[year] => 2016
)
[7] => Array
(
[eps] => -0.49
[year] => 2015
)
)
I am wondering to put data here as of without loop this is the output coming on
$prof_data[] = $data3 //This contains array value;
Out Put`
Array
(
[0] => Array
(
[0] => Array
(
[eps] => -0.28
[year] => 2015
[company_id] => 348
)
[1] => Array
(
[eps] => 3.33
[year] => 2014
[company_id] => 348
)
)
[1] => Array
(
[0] => Array
(
[eps] => 0.90
[year] => 2015
[company_id] => 351
)
[1] => Array
(
[eps] => 0.81
[year] => 2014
[company_id] => 351
)
)
[2] => Array
(
[0] => Array
(
[eps] => 1.05
[year] => 2016
[company_id] => 356
)
[1] => Array
(
[eps] => 3.71
[year] => 2015
[company_id] => 356
)
)
[3] => Array
(
[0] => Array
(
[eps] => 1.61
[year] => 2016
[company_id] => 366
)
[1] => Array
(
[eps] => -0.49
[year] => 2015
[company_id] => 366
)
)
[4] => Array
(
)
)
Now can anybody help me out to solve this issue I am not good while making to understand what I wam trying to do please let e know if you have queries
As I understand you can perform calculation as
foreach($data3 as $data){
$net = $currenteps - $data['eps'];
}
You haven't defined what are the currenteps and oldeps, however using $data['eps'] and $data['year'], you can access each eps value and respective year of array
I have a multidimensional array like this one ( each $orders[$userid] array has multiple arrays of orders)
foreach($orders[$userid] as $order){
print_r($order);
}
This is the first user's orders grouped by date,month,year
Array ( [id] => 409079 [user_id] => 26017 [total] => 30 [final_total] => 29.1 [order_status_id] => 1
[day] => 1 [month] => 11 [year] => 2016 [amount2] => 2198.2999696731567 )
Array ( [id] => 410744 [user_id] => 26017 [total] => 175 [final_total] => 165 [order_status_id] => 1
[day] => 2 [month] => 11 [year] => 2016 [amount2] => 2619.799982070923 )
Array ( [id] => 412268 [user_id] => 26017 [total] => 300 [final_total] => 293 [order_status_id] => 1
[day] => 3 [month] => 11 [year] => 2016 [amount2] => 4413.400000572205 )
Array ( [id] => 405860 [user_id] => 26017 [total] => 10 [final_total] => 9.8 [order_status_id] => 1
[day] => 30 [month] => 10 [year] => 2016 [amount2] => 352.5999994277954 )
Array ( [id] => 407500 [user_id] => 26017 [total] => 85 [final_total] => 84.5 [order_status_id] => 1
[day] => 31 [month] => 10 [year] => 2016 [amount2] => 1135.1000022888184 )
As you see older dated are displayed after recent dates. The question is how to sort this multidimensional array to display old dates then recent dates like this
Array ( [id] => 405860 [user_id] => 26017 [total] => 10 [final_total] => 9.8 [order_status_id] => 1
[day] => 30 [month] => 10 [year] => 2016 [amount2] => 352.5999994277954 )
Array ( [id] => 407500 [user_id] => 26017 [total] => 85 [final_total] => 84.5 [order_status_id] => 1
[day] => 31 [month] => 10 [year] => 2016 [amount2] => 1135.1000022888184 )
Array ( [id] => 409079 [user_id] => 26017 [total] => 30 [final_total] => 29.1 [order_status_id] => 1
[day] => 1 [month] => 11 [year] => 2016 [amount2] => 2198.2999696731567 )
Array ( [id] => 410744 [user_id] => 26017 [total] => 175 [final_total] => 165 [order_status_id] => 1
[day] => 2 [month] => 11 [year] => 2016 [amount2] => 2619.799982070923 )
Array ( [id] => 412268 [user_id] => 26017 [total] => 300 [final_total] => 293 [order_status_id] => 1
[day] => 3 [month] => 11 [year] => 2016 [amount2] => 4413.400000572205 )
Please use code:
foreach ($orders as $key => $row) {
$year[$key] = $row['year'];
$month[$key] = $row['month'];
$day[$key] = $row['day'];
}
array_multisort($year, SORT_ASC, $month, SORT_ASC, $day, SORT_ASC, $orders);
While in more complex situations you would use array_multisort, in this case, you can reduce the problem to sort only by one value. The
'year', 'month' and 'day' are basically parts of the date. Using usort function you can do the following:
$dateToTimestamp = function (array $order) {
$day = $order['day'];
$month = $order['month'];
$year = $order['year'];
return strtotime("$day-$month-$year");
};
usort($array, function ($a, $b) use ($dateToTimestamp) {
return $dateToTimestamp($a) - $dateToTimestamp($b);
});
One thing to pay attention with strtotime you cannot really set a locale in order to recognize which part of the string is a day, month, or year. In one of PHP manual comments, there is a trick to compensate this. In our case, the use of dash separator ensures the order.
Here is working demo.
How to unset all keys named id from a multidimensional array?
private function remove_id(Array &$arr){
foreach($arr as $key => $value){
if($key == 'id'){
unset($arr[$key]);
}
elseif(is_array($value)){
$this->remove_id($value);
}
}
}
array
Array
(
[id] => 52453
[periods] => Array
(
[0] => Array
(
[id] => 95296
[is_readonly] => 0
[year] => 2016
[month] => 1
)
[1] => Array
(
[id] => 95297
[is_readonly] => 0
[year] => 2016
[month] => 2
)
[2] => Array
(
[id] => 95298
[is_readonly] => 0
[year] => 2016
[month] => 3
)
[3] => Array
(
[id] => 95299
[is_readonly] => 0
[year] => 2016
[month] => 4
)
[4] => Array
(
[id] => 95300
[is_readonly] => 0
[year] => 2016
[month] => 5
)
[5] => Array
(
[id] => 95301
[is_readonly] => 0
[year] => 2016
[month] => 6
)
[6] => Array
(
[id] => 95302
[is_readonly] => 0
[year] => 2016
[month] => 7
)
[7] => Array
(
[id] => 95303
[is_readonly] => 0
[year] => 2016
[month] => 8
)
[8] => Array
(
[id] => 95304
[is_readonly] => 0
[year] => 2016
[month] => 9
)
[9] => Array
(
[id] => 95305
[is_readonly] => 0
[year] => 2016
[month] => 10
)
[10] => Array
(
[id] => 95306
[is_readonly] => 0
[year] => 2016
[month] => 11
)
[11] => Array
(
[id] => 95307
[is_readonly] => 0
[year] => 2016
[month] => 12
)
)
[closing_profit_amount] => 0
[closing_profit_net_amount] => 0
)
You need to use &value in the for loop to keep changes in nested arrays.
Also the array cannot have more than 1 id key, so there is no need to check it within the loop, which allow a bit of microoptimization here:
private function remove_id(Array &$arr){
if(isset($arr['id'])) {
unset($arr['id']);
}
foreach($arr as &$value){
if(is_array($value)){
$this->remove_id($value);
}
}
}