I have the following array depicting points accumulated during the year. As a tie breaker on equal points, I have another value that I calculate that I want the result to be sorted on.
This is the sorted array, showing points during the year:
$results = [
1 => 220
0 => 209
4 => 127
14 => 89
3 => 84
7 => 78
2 => 71
13 => 61
16 => 56
8 => 48
12 => 45
10 => 42
11 => 42
6 => 39
5 => 35
9 => 32
15 => 22
17 => 22
18 => 22
19 => 1
Because indexes 10 and 11, and 15, 17 and 18 are equal, they need to be sorted by the lowest values in the following array:
// For 10 and 11
$anotherArray = [
11 => 101
10 => 119
]
// For 15, 17 and 18
$anotherArray = [
17 => 150
18 => 160
15 => 179
]
So the resulting array should look like this:
$finalArray = [
1 => 220
0 => 209
4 => 127
14 => 89
3 => 84
7 => 78
2 => 71
13 => 61
16 => 56
8 => 48
12 => 45
11 => 42
10 => 42
6 => 39
5 => 35
9 => 32
17 => 22
18 => 22
15 => 22
19 => 1
]
How would I achieve this?
EDIT: It's not the same as this question. The solution suggested is based on the values of an array and does not solve the issue where I need to insert values in another array in the correct order.
A 2-dimensional array is created with the values of the result array and the values of the 'anotherArrays'.
This array is sorted with uasort () and converted back into a one-dimensional array.
$results = [
1 => 220,0 => 209,4 => 127, 14 => 89, 3 => 84, 7 => 78,
2 => 71, 13 => 61, 16 => 56, 8 => 48, 12 => 45, 10 => 42, 11 => 42,
6 => 39, 5 => 35, 9 => 32, 15 => 22, 17 => 22, 18 => 22, 19 => 1
];
$anotherArray1 = [11 => 101, 10 => 119];
$anotherArray2 = [17 => 150, 18 => 160, 15 => 179];
$sekArr = array_replace($results,$anotherArray1,$anotherArray2);
$newArr = [];
foreach($results as $key => $value){
$newArr[$key] = ["v" => $value, 's' => $sekArr[$key]];
}
uasort($newArr, function($a,$b){
return $b['v'] <=> $a['v'] ?: $a['s'] <=> $b['s'];
});
$results = [];
foreach($newArr as $key => $arr){
$results[$key] = $arr['v'];
}
var_dump($results);
Try for yourself: https://3v4l.org/Si6ST
Related
I have 2 arrays and need to find the unique values in the second array that are not in the first array and output those to a third array.
Array 1
$array1 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value79',
);
Array 2
$array2 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value81',
);
Output Array
$array3 = array(
0 => 'value81',
);
I have tried array_diff a few different ways and it only gives me an array of both array1 and array2 combined together.
UPDATE:
array_diff works fine on the example arrays above, but I can't get it to work for the actual arrays I'm using below.
Array 1
$array1 = array (
0 => 'e8B283B606A212d82036F74f88177375125440F6',
1 => '633AD0A20588237b6908Ee35B23be06bf5c17b90',
2 => '81C025abAc1a3d51dfcae7a2C226ADD463B794d7',
3 => '1fEc22B25E2200cA88D30B6Ba1d8B5696b2b7D52',
4 => 'AA7A7C2DECB180f68F11E975e6D92B5Dc06083A6',
5 => '09a054B60bd3B908791B55eEE81b515B93831E99',
6 => 'f155825650d85fdc50a2e7bbc27b35b666f4b0a4',
7 => '7e42976faf925dbacedd116a2b6a2bb5c9adef4f',
8 => '556754785B5428cB5C04B798Ea993e5d3c550A66',
9 => '5a4904f474077a021eee8829f7d11d50fe76993f',
10 => '36C39434dB7987Cae67233D06de9427c4C67E787',
11 => '3c1ddb1fb17d83788039d5750c1136017c637a55',
12 => '0f94e2a44ced2a368da8a1edeaa337f13a8560d0',
13 => '05227e4FA98a6415ef1927E902dc781AA7eD518a',
14 => 'EC2E833E90654EE6DD71e7b9EA3bE8B97299CC03',
15 => 'B279F56B1F71F85d59Dc1499E697E4F005298c14',
16 => '73d4F0e3eD1dE3BD278a71C02Adbaf6AD51A610C',
17 => '7D10f0fa8aB734328718212f21Aa8018CCcEd0f4',
18 => 'C9040Ca5e0A7c675343facbD28fe698594168522',
19 => '79F8fFa1502EE1DD6602c2459ECE6960D46012Ac',
20 => '66EA40Bb3F67b8Dc101fB01e6273A4F913Aac559',
21 => 'eC2A9A192929ed1f280f7Fd8935b881e3a649dc0',
22 => 'ba944AbD6Ad261B220dcc8ba6b53410Cb024FCB1',
23 => 'edb477Ca9ADE4d1152666eE914cB279179ffED92',
24 => 'C683946C5dF8eC48D20d64CaF58257b6135faEbB',
25 => '949990EccC1735681C6dFc64516aC882e4948cE2',
26 => '5a6dc9dbce3FdDfDE2905B7ce45b87BFe5bd6872',
27 => '177A56bb19024737C945eb040902fe2124c63fc2',
28 => 'cfd090de8cc555a48b5e742325d88c5937b9074c',
29 => '91671d0C02c7465b0948e819d2C2d261405301cF',
30 => 'A5c5f7fF2186898283e000352770e1cb140d1a00',
31 => 'Db5a91E6f925A22FE995BfB403eacc74DFE34F04',
32 => '26d13C17659e1305344eE31635B5A2b72e94d82E',
33 => '136f4b5b6a306091b280e3f251fa0e21b1280cd5',
34 => '4cdc990d2EE172534d503Fec256B16B5fD09394f',
35 => '04EeA222B3CC6F1F667915E952539eCa8468b3FE',
);
Array 2
$array2 = array (
0 => '28697b4a1618a77b1d61a85e99174616b519f629',
1 => 'e8b283b606a212d82036f74f88177375125440f6',
2 => '633ad0a20588237b6908ee35b23be06bf5c17b90',
3 => '81c025abac1a3d51dfcae7a2c226add463b794d7',
4 => 'fead04e9b311eebdc5f1e963b7472af15b21430d',
5 => '23d3a453c7bd4634b81219d63b12d5e3ceec04f6',
6 => '716f412c236236845d8592873bd1e78d024fadf5',
7 => '460eebde20336aa615370f4b7b7a916e5656d4e6',
8 => '5ff58e03ac620166f6093f85c2edec521fac7a26',
9 => 'f155825650d85fdc50a2e7bbc27b35b666f4b0a4',
10 => '556754785b5428cb5c04b798ea993e5d3c550a66',
11 => '5a4904f474077a021eee8829f7d11d50fe76993f',
12 => 'e8577ff0c2a982b1550993a40c34cf26ad4732ff',
13 => '36c39434db7987cae67233d06de9427c4c67e787',
14 => '1fec22b25e2200ca88d30b6ba1d8b5696b2b7d52',
15 => '7e42976faf925dbacedd116a2b6a2bb5c9adef4f',
16 => '3c1ddb1fb17d83788039d5750c1136017c637a55',
17 => 'd7644fa6fe75eb7338c46807a640fa35b2c6dd12',
18 => '9175e0e8434cb6c5cc45a64f8a80f66ec81b47bf',
19 => '91c700c233491fad57ee7b91e0a5de5cbbb63080',
20 => 'ec2e833e90654ee6dd71e7b9ea3be8b97299cc03',
21 => '73d4f0e3ed1de3bd278a71c02adbaf6ad51a610c',
22 => '6522bbd602e1cdb3c1e12a04c8a5d1a9f5352a1e',
23 => '92598e499f4c576104105279cf926df447f2d044',
24 => 'bb826a44f1c8069207248d34a56ce05bc72d2901',
25 => '66ea40bb3f67b8dc101fb01e6273a4f913aac559',
26 => '7d10f0fa8ab734328718212f21aa8018ccced0f4',
27 => 'f7435817b7d4165cbf7b56539426267cd8872895',
28 => '73ddf33aff4274dc706241c4a1263196e5e6e872',
29 => 'edb477ca9ade4d1152666ee914cb279179ffed92',
30 => 'c683946c5df8ec48d20d64caf58257b6135faebb',
31 => '949990eccc1735681c6dfc64516ac882e4948ce2',
32 => '5a6dc9dbce3fddfde2905b7ce45b87bfe5bd6872',
33 => 'f0e6f7b1a9e917cdd76981b20265719c0b96875c',
34 => 'a7c4cea7f9263ceabb958118636929521b61ec80',
35 => '2ad41576364037cf25fd08948f425b63610d6cf3',
36 => 'cc73a3c13fe628823b6759ad07199794a252a41d',
37 => 'b30a9afb76e4ba949f5523a28bda7ade35e6f5d0',
38 => '650ff5efc2f881236d3079e465cc3e087c6ddca5',
39 => '8dc82ddbf1efb4befcb577b1c0a6efb812a0c562',
40 => '1b2a811c808d30aa2a0270b6729609a84a6b9e82',
41 => '442b7055dd9b2c483884ad250734172b2ae7fc76',
42 => 'c9040ca5e0a7c675343facbd28fe698594168522',
43 => 'e32bb01574b9a36bf16884cc7160977efc9dd6b3',
44 => 'caad2020967f0f314fb8a150413f7f9fc26c0f73',
45 => 'ad4273e44ce8896c943ee6851d96a307b14205e5',
46 => '6f0784344fe67eaf46d74171f5469274b690648f',
47 => '81036acab4da3d559fe11299d48d43b3d31551a7',
48 => '0f94e2a44ced2a368da8a1edeaa337f13a8560d0',
);
code:
$result = array_diff($array2, $array1);
echo '<pre>' . var_export($result, true) . '</pre><br>';
Did you try with array_diff() or array_diff_assoc() by swapping the parameter? ,
<?php
$array1 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value79',
);
$array2 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value81',
);
$result=array_diff_assoc($array2,$array1);
print_r($result);
?>
WORKING DEMO: https://3v4l.org/aNjUm
You can achieve the desired array by array_diff as
<?php
$array1 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value79',
);
$array2 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value81',
);
$value = array_diff($array2, $array1);
$array3[] = $value[3];
print_r($array3);
?>
Output
Array
(
[0] => value81
)
Note: If you swap the parameters of array_diff function you will get diff results as
1:
$value = array_diff($array2, $array1);
//Output
Array
(
[0] => value81
)
2:
$value = array_diff($array1, $array2);
//Output
Array
(
[0] => value79
)
The issue is case sensitivity in my actual arrays that I posted in the Update of my question.
This is what ended up working for me. I used the array_udiff with a callback function of strcasecmp. And this gives me the desired result.
strcasecmp ignores case and treats A as a or B as b and vise versa.
$result = array_udiff($array2, $array1, 'strcasecmp');
I have an array with multiple similar minimum value.
May I know how to randomly get one of the minimum value?
Here is my sample code:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
$intNo = $b[0];
May I know how to get min value list (3 => 12, 4 => 12,5 => 12,10 => 12) and randomly pick one of them so that I can set in $intNo?
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
// Taking a random KEY from $b
$key = array_rand($b);
// Taking a KEY from $aryNo which is under `$key`
echo $b[$key];
// Taking a VALUE from `$aryNo` which is under `$b[$key]`
echo $aryNo[$b[$key]];
The fiddle.
Try something like this:
$aryNo = [34,34,34,51,12,12,12,56,876,453,43,12];
foreach($aryNo as $a) {
$finalArray[$a][] = $a;
}
print("<pre>".print_r($finalArray,true)."</pre>");
$minKey = min(array_keys($finalArray));
print("<pre>".print_r($finalArray[$minKey],true)."</pre>");
$randIndex = array_rand($finalArray[$minKey]);
print_r("Key: ".$randIndex.", ".$finalArray[$minKey][$randIndex]);
First print prints:
Array
(
[34] => Array
(
[0] => 34
[1] => 34
[2] => 34
)
[51] => Array
(
[0] => 51
)
[12] => Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
[56] => Array
(
[0] => 56
)
[876] => Array
(
[0] => 876
)
[453] => Array
(
[0] => 453
)
[43] => Array
(
[0] => 43
)
)
Than you select min key, and that prints this:
Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
At the end you pick random key from this array and print the value:
Key: 2, Value: 12
`<?php
$sortArr = array();
$aryNo = array(0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,5 =>
12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,10 => 12);
asort($aryNo);
$aryNo = array_values($aryNo);
print_r($aryNo);
echo $aryNo[0];
?>`
I found that if I using shuffle(); also work for me.
Here is my example:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$aryNo2 = array_keys($aryNo, min($aryNo));
shuffle($aryNo2); //
$intWinNo = $aryNo2[0];
Thanks #u_mulder suggestion & answer.
I'm trying to count integer value from JSON: https://pomber.github.io/covid19/timeseries.json and got what I expected.
{
"date": [
"22 Jan",
"23 Jan",
"24 Jan",
"25 Jan",
....
],
"total_confirmed": [
555,
653,
941,
1434,
....
],
"total_deaths": [
17,
18,
26,
42,
....
],
"total_recovered": [
28,
30,
36,
39,
....
],
"max_value_of_total_confirmed": 272166
}
Here's my controller:
$client = new Client();$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
$confirmed_array = array();
$deaths_array = array();
$recovered_array = array();
if ( ! empty( $posts_dates ) ) {
foreach ( $posts_dates as $country => $data ) {
foreach ( $data as $dataKey => $dateData ) {
$date = new \DateTime( $dateData['date'] );
$day = $date->format( 'd M' );
if ( !isset($confirmed_array[$day]) ) {
$confirmed_array[$day] = 0;
}
$confirmed_array[$day] += $dateData['confirmed'];
}
foreach ( $data as $dataKey => $dateData ) {
$date = new \DateTime( $dateData['date'] );
$day = $date->format( 'd M' );
if ( !isset($deaths_array[$day]) ) {
$deaths_array[$day] = 0;
}
$deaths_array[$day] += $dateData['deaths'];
}
foreach ( $data as $dataKey => $dateData ) {
$date = new \DateTime( $dateData['date'] );
$day = $date->format( 'd M' );
if ( !isset($recovered_array[$day]) ) {
$recovered_array[$day] = 0;
}
$recovered_array[$day] += $dateData['recovered'];
}
}
}
$output = [
"date" => array_keys($confirmed_array),
"total_confirmed" => array_values($confirmed_array),
"total_deaths" => array_values($deaths_array),
"total_recovered" => array_values($recovered_array),
"max_value_of_total_confirmed" => max($confirmed_array)
];
return $output;
What I'm trying to get is grouping those data not by date, but by week of the month (W3 Jan, W4 Jan, W1 Feb, W2 Feb, etc). Any help would be appreciated :)
Thank you.
I would recommend using collection. You can easily manage using the methods provided by Laravel.
Group By Day
$client = new Client();
$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
/****** Here we go ******/
$posts_dates = collect($posts_dates)
->flatten(1)
->groupBy('date')
->map(function ($item, $key) {
$item = collect($item);
$date = \Carbon\Carbon::parse($key);
$key = 'W' . $date->weekOfMonth . $date->format(' M');
return [
'date' => $key,
'total_confirmed' => $item->sum('confirmed'),
'total_deaths' => $item->sum('deaths'),
'total_recovered' => $item->sum('recovered'),
];
})
->groupBy(function ($item, $key) {
return collect($item)->keys()->all();
})
->map(function ($item, $key) {
return $item->map(function ($item) use ($key) {
return $item[$key];
});
});
$posts_dates->put('max_value_of_total_confirmed', $posts_dates['total_confirmed']->max());
dd($posts_dates->toArray());
Result
array:5 [
"date" => array:59 [
0 => "W4 Jan"
1 => "W4 Jan"
2 => "W4 Jan"
3 => "W4 Jan"
4 => "W4 Jan"
5 => "W4 Jan"
6 => "W4 Jan"
7 => "W5 Jan"
8 => "W5 Jan"
9 => "W5 Jan"
10 => "W1 Feb"
11 => "W1 Feb"
12 => "W1 Feb"
13 => "W1 Feb"
14 => "W1 Feb"
15 => "W1 Feb"
16 => "W1 Feb"
17 => "W2 Feb"
18 => "W2 Feb"
19 => "W2 Feb"
20 => "W2 Feb"
21 => "W2 Feb"
22 => "W2 Feb"
23 => "W2 Feb"
24 => "W3 Feb"
25 => "W3 Feb"
26 => "W3 Feb"
27 => "W3 Feb"
28 => "W3 Feb"
29 => "W3 Feb"
30 => "W3 Feb"
31 => "W4 Feb"
32 => "W4 Feb"
33 => "W4 Feb"
34 => "W4 Feb"
35 => "W4 Feb"
36 => "W4 Feb"
37 => "W4 Feb"
38 => "W5 Feb"
39 => "W1 Mar"
40 => "W1 Mar"
41 => "W1 Mar"
42 => "W1 Mar"
43 => "W1 Mar"
44 => "W1 Mar"
45 => "W1 Mar"
46 => "W2 Mar"
47 => "W2 Mar"
48 => "W2 Mar"
49 => "W2 Mar"
50 => "W2 Mar"
51 => "W2 Mar"
52 => "W2 Mar"
53 => "W3 Mar"
54 => "W3 Mar"
55 => "W3 Mar"
56 => "W3 Mar"
57 => "W3 Mar"
58 => "W3 Mar"
]
"total_confirmed" => array:59 [
0 => 555
1 => 653
2 => 941
3 => 1434
4 => 2118
5 => 2927
6 => 5578
7 => 6166
8 => 8234
9 => 9927
10 => 12038
11 => 16787
12 => 19881
13 => 23892
14 => 27635
15 => 30817
16 => 34391
17 => 37120
18 => 40150
19 => 42762
20 => 44802
21 => 45221
22 => 60368
23 => 66885
24 => 69030
25 => 71224
26 => 73258
27 => 75136
28 => 75639
29 => 76197
30 => 76823
31 => 78579
32 => 78965
33 => 79568
34 => 80413
35 => 81395
36 => 82754
37 => 84120
38 => 86011
39 => 88369
40 => 90306
41 => 92840
42 => 95120
43 => 97882
44 => 101784
45 => 105821
46 => 109795
47 => 113561
48 => 118592
49 => 125865
50 => 128343
51 => 145193
52 => 156094
53 => 167446
54 => 181527
55 => 197142
56 => 214910
57 => 242708
58 => 272166
]
"total_deaths" => array:59 [
0 => 17
1 => 18
2 => 26
3 => 42
4 => 56
5 => 82
6 => 131
7 => 133
8 => 171
9 => 213
10 => 259
11 => 362
12 => 426
13 => 492
14 => 564
15 => 634
16 => 719
17 => 806
18 => 906
19 => 1013
20 => 1113
21 => 1118
22 => 1371
23 => 1523
24 => 1666
25 => 1770
26 => 1868
27 => 2007
28 => 2122
29 => 2247
30 => 2251
31 => 2458
32 => 2469
33 => 2629
34 => 2708
35 => 2770
36 => 2814
37 => 2872
38 => 2941
39 => 2996
40 => 3085
41 => 3160
42 => 3254
43 => 3348
44 => 3460
45 => 3558
46 => 3802
47 => 3988
48 => 4262
49 => 4615
50 => 4720
51 => 5404
52 => 5819
53 => 6440
54 => 7126
55 => 7905
56 => 8733
57 => 9867
58 => 11299
]
"total_recovered" => array:59 [
0 => 28
1 => 30
2 => 36
3 => 39
4 => 52
5 => 61
6 => 107
7 => 126
8 => 143
9 => 222
10 => 284
11 => 472
12 => 623
13 => 852
14 => 1124
15 => 1487
16 => 2011
17 => 2616
18 => 3244
19 => 3946
20 => 4683
21 => 5150
22 => 6295
23 => 8058
24 => 9395
25 => 10865
26 => 12583
27 => 14352
28 => 16121
29 => 18177
30 => 18890
31 => 22886
32 => 23394
33 => 25227
34 => 27905
35 => 30384
36 => 33277
37 => 36711
38 => 39782
39 => 42716
40 => 45602
41 => 48228
42 => 51170
43 => 53796
44 => 55865
45 => 58358
46 => 60694
47 => 62494
48 => 64404
49 => 67003
50 => 68324
51 => 70251
52 => 72624
53 => 76034
54 => 78088
55 => 80840
56 => 83207
57 => 84854
58 => 87256
]
"max_value_of_total_confirmed" => 272166
]
Group By Week
$client = new Client();
$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
/****** Here we go ******/
$posts_dates = collect($posts_dates)
->flatten(1)
->map(function ($item) {
$date = \Carbon\Carbon::parse($item['date']);
$item['week'] = 'W' . $date->weekOfMonth . $date->format(' M');
return $item;
})
->groupBy('week')
->map(function ($item, $key) {
$item = collect($item);
return [
'date' => $key,
'total_confirmed' => $item->sum('confirmed'),
'total_deaths' => $item->sum('deaths'),
'total_recovered' => $item->sum('recovered'),
];
})
->groupBy(function ($item, $key) {
return collect($item)->keys()->all();
})
->map(function ($item, $key) {
return $item->map(function ($item) use ($key) {
return $item[$key];
});
});
$posts_dates->put('max_value_of_total_confirmed', $posts_dates['total_confirmed']->max());
dd($posts_dates->toArray());
Result
array:5 [
"date" => array:10 [
0 => "W4 Jan"
1 => "W5 Jan"
2 => "W1 Feb"
3 => "W2 Feb"
4 => "W3 Feb"
5 => "W4 Feb"
6 => "W5 Feb"
7 => "W1 Mar"
8 => "W2 Mar"
9 => "W3 Mar"
]
"total_confirmed" => array:10 [
0 => 14206
1 => 24327
2 => 165441
3 => 337308
4 => 517307
5 => 565794
6 => 86011
7 => 672122
8 => 897443
9 => 1275899
]
"total_deaths" => array:10 [
0 => 372
1 => 517
2 => 3456
3 => 7850
4 => 13931
5 => 18720
6 => 2941
7 => 22861
8 => 32610
9 => 51370
]
"total_recovered" => array:10 [
0 => 353
1 => 491
2 => 6853
3 => 33992
4 => 100383
5 => 199784
6 => 39782
7 => 355735
8 => 465794
9 => 490279
]
"max_value_of_total_confirmed" => 1275899
]
Here is my array . I want to sort the array according the value of each key
Input array:-
Array
(
[location_classroom] => 209
[location_daily_pe] => 1
[location_hallways] => 3
[location_playground] => 93
[location_shade_area] => 26
[location_specialist] => 8
[location_toilet] => 3
[location_others] => 27
[location_others_info] => 0
)
Output array:-
Array
(
[location_others_info] => 0
[location_daily_pe] => 1
[location_hallways] => 3
[location_toilet] => 3
[location_specialist] => 8
[location_shade_area] => 26
[location_playground] => 93
[location_classroom] => 209
[location_others] => 27
)
Indeed you should use asort():
$arr = [
'location_classroom' => 209,
'location_daily_pe' => 1,
'location_hallways' => 3,
'location_playground' => 93,
'location_shade_area' => 26,
'location_specialist' => 8,
'location_toilet' => 3,
'location_others' => 27,
'location_others_info' => 0
];
asort($arr);
# but you can't print the output of the sorting - it'll give you nothing meaningful (boolean)
# you should print the sorted array itself
print_r($arr);
OUTPUT
Array
(
[location_others_info] => 0
[location_daily_pe] => 1
[location_toilet] => 3
[location_hallways] => 3
[location_specialist] => 8
[location_shade_area] => 26
[location_others] => 27
[location_playground] => 93
[location_classroom] => 209
)
How can I get max key from same value?
Example:
1 => 32
2 => 32
3 => 32
4 => 5
5 => 5
6 => 11
7 => 11
Result:
3 => 32
5 => 5
7 => 11
Assuming values are all integer
$array = [
1 => 32,
2 => 32,
3 => 32,
4 => 5,
5 => 5,
6 => 11,
7 => 11,
];
$result = array_flip(array_flip($array));
var_dump($result);
krsort($array);
ksort(array_unique($array);