Reversing PHP array ksort - php

I have an array which is like this:
Array(
[31] => 1
[30] => 2
[29] => 3
[28] => 4
)
I then use ksort($array) which sorts it as 28, 29, 30, and 31 but the problem with this is the numbers 1-4 go with the values so get reversed. I want 28 to become 1, 29 to become 2 etc.
Is there a way without creating a foreach loop and reconstructing a new array to make this switch?

You can flip the array, sort it, and then flip it back:
$array = array(31 =>1, 30 => 2, 29 => 3, 28 => 4);
$result = array_flip($array);
sort($result);
$result = array_flip($result);
This results in an array sorted by keys, and values integers starting from 0:
Array (
[28] => 0
[29] => 1
[30] => 2
[31] => 3
)
Maintaining existing values
If you want to maintain your existing values then use the array_combine function to merge the sorted keys with the old values:
$result = array_flip($array);
sort($result);
$result = array_combine($result, $array);
The resulting array is then:
Array
(
[28] => 1
[29] => 2
[30] => 3
[31] => 4
)

Related

PHP Sort Ascending Keeping Identical Keys

I am working with a php associative array and i want to sort in ascending order on array key...But it is removing identical keys and keeping one key.
Here is my code
<?php
$num = [
100 => 75,
45 => 89,
120 => 101,
120 => 121,
120 => 11,
];
asort($num );
print_r($num);
But result is Array ( [120] => 11 [100] => 75 [45] => 89 )
I want the output is ascending manner Array ( [120] => 11 [100] => 75 [45] => 89 [120] => 121 [120] => 101)
I want to keep all 3 key value pair with the key 120. How can i do it?
What you want is not valid in PHP; arrays cannot have more than one value with the same key. One alternative would be to use a multi-dimensional array, for example:
$num = [
[100 => 75],
[45 => 89],
[120 => 101],
[120 => 121],
[120 => 11],
];
This can then be sorted with usort:
usort($num, function ($a, $b) { return reset($a) - reset($b); });
Output:
Array
(
[0] => Array
(
[120] => 11
)
[1] => Array
(
[100] => 75
)
[2] => Array
(
[45] => 89
)
[3] => Array
(
[120] => 101
)
[4] => Array
(
[120] => 121
)
)
Demo on 3v4l.org
You can work with this array using a foreach loop, for example:
foreach ($num as $arr) {
echo key($arr) . ' => ' . reset($arr) . PHP_EOL;
}
Output:
120 => 11
100 => 75
45 => 89
120 => 101
120 => 121
Demo on 3v4l.org
An array key is by definition of being a key, unique. You can not have multiple parts of data saved under the same key. In PHP the later values simply overwrite the previous ones.
From the Manual:
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
What you can do, is have that key full of sub-array of other values. This will require the method of creating your current data structure to be updated but could output something like:
So;
$num = [
100 => 75,
45 => 89,
120 => [ 0 => 101,
1 => 121,
2 => 11]
];
asort($num );
print_r($num);
I would give you an example of how to construct this but would like to have your own code to work from first.

How to modify an array so that i want to get an array with an incremented value of some array values [duplicate]

This question already has answers here:
How to push both value and key into PHP array
(21 answers)
Closed 3 years ago.
I come across a scenario where i have an array like this
Array
(
[24] => 24
[25] => 25
[26] => 26
[27] => 27
[28] => 28
[29] => 29
)
But i want a resultant array by including the incremented value of last and decremented value of first like this
Array
(
[24] => 24
[25] => 25
[26] => 26
[27] => 27
[28] => 28
[29] => 29
[23] => 23
[30] => 30
)
I know this is a little bit strange. But can anyone help me to find the result.
I found out the values
$f=reset($res);
$f=$f-1;
$l=end($res);
$l=$l+1;
but no idea on how to push into array
It's probably safer to use min and max to find the lowest and highest values, otherwise your code wouldn't work on your result array (reset($array) on the output still returns 24, not 23):
$min = min($array) - 1;
$max = max($array) + 1;
$array[$min] = $min;
$array[$max] = $max;
print_r($array);
Output:
Array
(
[24] => 24
[25] => 25
[26] => 26
[27] => 27
[28] => 28
[29] => 29
[23] => 23
[30] => 30
)
Demo on 3v4l.org
Under the condition that the key is always equal to the value you can try that:
$data = [
24 => 24,25 => 25,26 => 26,27 => 27,28 => 28,29 => 29,
];
$first = reset($data)-1;
$end = end($data)+1;
$data[$first] = $first;
$data[$end] = $end;
But I think you do not want the first and last, but min and max.

How to find 5 biggest value from php array with original keys?

This array keys is a database primary keys and i need to find biggest value from array and after that update query which need this primary keys. so i need to it will be remain same keys.
My array
Array ( [1] => 7 [2] => 2 [3] => 2 [4] => 10 [5] => 15 [6] => 11 [7] => 40 )
i need this output
Array ( [7] => 40 [5] => 15 [6] => 11 [4] => 10 [1] => 7 )
The PHP arsort function will sort your array in descending order and maintain the indices.
$array = array ( 1 => 7, 2 => 2, 3 => 2, 4 => 10, 5 => 15, 6 => 11, 7 => 40 );
arsort($array);
print_r($array);
Demo: eval.in/836445https://3v4l.org/KQB5Y
To limit it to the top five use array_slice:
print_r(array_slice($array, 0, 5, true));
https://3v4l.org/DSkRt

PHP descending an array stored from foreach loop values

i have a foreach loop like this
$ids = array();
foreach( $idsvalues as $idv ){
$ids[$idv->id][] = $idv->value;
}
and i get an array like this
Array ( [21] => 10 [13] => 16 [12] => 20 [7] => 28 )
now how do i descending this array() values only from lowest to highest without effecting the array keys or id?
to show like this
Array ( [21] => 28 [13] => 20 [12] => 16 [7] => 10 );
the array can contain upto 100 such ids and values so basically just descending the values?
Since you want to preserve the keys, transfer them to a separate array by using array_keys now rsort your array (i.e. descending order) , Make use of array_combine to link the grabbed keys and descending sorted values.
<?php
$arr=Array ( 21 => 10, 13 => 16, 12 => 20, 7 => 28 );
$k_arr=array_keys($arr);
rsort($arr);
$new_arr=array_combine($k_arr,$arr);
print_r($new_arr);
Output:
Array
(
[21] => 28
[13] => 20
[12] => 16
[7] => 10
)
Demo

How to get "maximum range" from an array

Index is 00 to 23 (24-hour time)
What I am trying to accomplish is to get largest maximum range.
Each array will have a maximum of 24 elements. Of these, I want to get all the ones which are high (a range).
So,
for first array - 10,16,19,19
second - 18,19,20
third - 9,11.
Array
(
[00] => 10
[01] => 19
[02] => 10
[03] => 4
[04] => 1
[13] => 16
[14] => 2
[15] => 5
[16] => 2
[17] => 3
[18] => 1
[19] => 1
[20] => 1
[21] => 5
[22] => 1
[23] => 2
)
Array
(
[09] => 6
[10] => 20
[11] => 18
[12] => 19
[13] => 3
[15] => 11
[16] => 9
[18] => 10
)
Array
(
[00] => 4
[01] => 3
[12] => 4
[16] => 4
[21] => 9
[22] => 11
[23] => 6
)
The problem is, these values could get changed entirely, like this one -
Array
(
[13] => 117
[14] => 221
[15] => 211
[16] => 145
[17] => 23
[18] => 15
[19] => 1
)
Any solution for this?
Thank you,people.
It's a bit unclear how you want to choose which values are 'high', but:
In PHP 5.3+
$filetered = array_filter($yourArray, function($v) {return $v > 10;});
Will return array with values higher than 10.
In PHP <5.3 you will need to createa callback function instead of passing a closure to array_filter.
Use array_keys() and than max() to get the maximum key value:
$keys = array_keys($myArray);
$maxKey = max($keys);
I think what you want (although it's not very clear what is a "high" value) can be accomplished with asort(). If you sort your array numerically:
asort($yourarray, SORT_NUMERIC);
Then you can just retrieve the first elements from the sorted array. You don't even specify how many elements should be retrieved, but I'm guessing it's 3, although you don't seem to want anything below 10, so just use array_slice() to extract the last three elements and if any of them is lower than 10 discard it.
Putting it all together would give something like:
asort($yourarray, SORT_NUMERIC);
$top3 = array_slice($yourarray, -3, null, true)
$filtered = array_filter($top3, function($v) {return $v > 10;});
Note: I used Mchl example on the final part of my answer.
You can use array_filter() to filter out those values that are not within the range:
function is_between_10_and_20($v)
{
return ($v >= 10 && $v <= 20);
}
$result = array_filter($arr, "is_between_10_and_20")

Categories