How to sort an array by numerical value? [duplicate] - php

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 1 year ago.
Consider this array:
Array( [Wingspan] => 5
[Scythe] => 1
[Spirit Island] => 2
[Everdell] => 1)
How can I sort this array with the highest value(5) as first, then 2 then lowest (1) as the last? I used this:
print_r(rsort($_POST['item']), SORT_NUMERIC);
But I get this:
Array
(
[0] => 2
[1] => 1
[2] => 1
[3] => 1
)
It changes the key, but this is wrong. I expect to get this:
Array( [Wingspan] => 5
[Spirit Island] => 2
[Scythe] => 1
[Everdell] => 1)
I searched and found this: Sort an Array by numerical value but that did not help.

Use the method
arsort($_POST['item'])
instead of rsort. rsort only works for simple arrays, not for associative ones. But anyways usually if the order of te items matters to you, probably an associative array is not the best choice. You could use a simple array with objects inside containing the values and the keys at once

Related

php unset array element while keeping numbers in order [duplicate]

This question already has answers here:
How to reindex an array?
(6 answers)
Closed 7 years ago.
I apologize in advance for this question - I'm sure it's been answered before, (hell, I've done it before), I just can't find anything in the search and my brain is drawing a blank...
I have an array in PHP where print_r outputs:
Array
(
[0] => VALUE
[1] => VALUE
[2] => VALUE
[3] => VALUE
[4] => VALUE
)
I use unset($_SESSION['item'][$lineID]); to unset a particular line from the array, and I'm left with:
Array
(
[0] => VALUE
[1] => VALUE
[3] => VALUE
[4] => VALUE
)
Element [2] has been removed, so the resulting array is no longer numerically in order
(e.g. - [0][1][3][4] instead of [0][1][2][3] ).
What PHP command do I run on the resulting array to "take out blanks" and reformat it so that it is numerically in order from 1 to n
To reset array keys after unset()
$array = array_values($array);

PHP - Renumbering Array Keys After Unsetting Value [duplicate]

This question already has answers here:
How to re-index all subarray elements of a multidimensional array?
(6 answers)
Closed 2 years ago.
I use a PHP array to store data about all the people a user is following on a website. Here is an example of how I have it set up:
$data = array(
['user1'] => array(
[0] => 'somedata',
[1] => 'moredata',
[2] => array(
[0] => 'Jim',
[1] => 'Bob',
[2] => 'Nick',
[3] => 'Susy',
)
),
);
As you can see, it is $data[user][2] that lists all the friends. The array has this exact appearance with [0] and [1] for keys because that is how var_export() does it. Now my problem is this. When someone unfollows somebody, I use unset() to delete that friend from the array. So if I want to unfollow Bob in the example above, it would be left with Jim, Nick, and Susy.
The only issue now is that the array keys do not renumber properly when they rename. So once Bob is gone it goes from 0 to 2 rather than Nick taking on the array key of 1. Now I can think of ways to do this myself but I would highly prefer if there were some PHP function specifically for solving this issue, that is, renaming these array keys to the proper numerical order. I checked out the sort() function but that seems for alphabetizing array values not keys.
You can use array_values to re index the array numerically.
$newArray = array_values($array);
If you just want to re-index the array at that level, you could simply use array_values();
For example, assuming you are removing the "bob" entry, just call array_values at the level directly above bob after removing it.
unset($data['user1'][2][1]);
$data['user1'][2] = array_values($data['user1'][2]);
I'd use array_values like this:
$data['user1'][2]=array_values($data['user1'][2]);
Here's the full code:
$data = array(
'user1' => array(
'somedata',
'moredata',
array(
'Jim',
'Bob',
'Nick',
'Susy',
)
),
);
unset($data['user1'][2][1]);
var_export ($data['user1'][2]);
echo "\n\n";
$data['user1'][2]=array_values($data['user1'][2]);
var_export($data['user1'][2]);
Result
array (
0 => 'Jim',
2 => 'Nick',
3 => 'Susy',
)
array (
0 => 'Jim',
1 => 'Nick',
2 => 'Susy',
)
See it in action here:
Sandbox
You could use array_splice
$removedElement = array_splice($data['user1'][2], $indexOfUserToRemove, 1);
This alters the original array reindexing it, but only if the keys of the array are numeric.

php sort array keys in multidimensional array [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
below is my array
$myarray = Array(
[1] => Array (['mytime']=>1),
[7] => Array(['mytime']=>2),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4)
);
I want to sort output of this array based on keys...
$myarray = Array(
[1] => Array (['mytime']=>1),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4),
[7] => Array(['mytime']=>2)
);
I have already tried ksort($myarray) it displays 1
anyways to fix this??
ksort() does this:
ksort($myarray);
Note: sorting functions do not return a new sorted array; they simply sort the array passed, and return true or false. Thus, ksort($myarray) will return 1 when successful, and $myarray will be sorted.
It's very clear if you read the docs: http://php.net/manual/en/function.ksort.php

Sort associative array according to value without deleting keys [duplicate]

This question already has answers here:
Sort array in PHP by value and maintain index association
(2 answers)
Closed 5 years ago.
I have an array which i want to sort on the basis of its value. But when i'm using rsort, it deletes all the keys. I tried to flip array and then used krsort, but then it removes some key/values pairs which have the same key.
Array
(
[533ae5a78ead0e8e118b4567] => 1
[534d5a4b8ead0e5b73294d72] => 45
[533ee8bc8ead0ec5138b4567] => 32
[535f42748ead0ef72ec72731] => 1
[537cc7128ead0e683071f3c0] => 2
[5388795b8ead0ea32f208680] => 3
[538c4f1a8ead0e75472f05b0] => 6
[538963758ead0e6759208680] => 5
[538961a58ead0e0459208680] => 3
[5389616e8ead0ecc58208680] => 3
[538962c68ead0eb6582098d8] => 2
[538964c78ead0ec159208680] => 1
[53887efc8ead0e2b35208680] => 1
[538964678ead0ea659208680] => 3
)
How to achieve that?
You can use arsort(). From the manual:
arsort — Sort an array in reverse order and maintain index association
Example:
$a = array(
'533ae5a78ead0e8e118b4567' => 1,
'534d5a4b8ead0e5b73294d72' => 45,
'533ee8bc8ead0ec5138b4567' => 32
);
arsort($a);
var_dump($a);
Output:
array(3) {
'534d5a4b8ead0e5b73294d72' =>
int(45)
'533ee8bc8ead0ec5138b4567' =>
int(32)
'533ae5a78ead0e8e118b4567' =>
int(1)
}

PHP Aligning Array Key Values

I've Googled it for two days, and tried looking at the PHP manual, and I still can't remember that function that aligns the key values for PHP arrays.
All I'm looking for is the function that takes this:
Array
(
[0] => 1
[3] => 2
[4] => 3
[7] => 4
[9] => 5
)
And converts it into this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Basically, the array is first sorted by key (their values attached to them stay with them), then all the keys are set to all the counting numbers, consecutively, without skipping any number (0,1,2,3,4,5,6,7,8,9...). I saw it being used with ksort() a few months ago, and can't see to remember or find this elusive function.
Well, you see, this one is hard, because the general description on the PHP array functions page does not say that this function does what you're looking for.
But you can sort the array using ksort(), and then use this: array_values() . From the page from the PHP manual:
array_values() returns all the values from the input array and indexes numerically the array.
You can use array_merge:
$array = array_merge($array);
It will reindex values with numeric keys.
Update: Using array_values as proposed in #LostInTheCode's answer is probably more descriptive.
function array_reset_index_keys($array)
{
$return = array();foreach($array as $k => $v){$return[] = $v;}return $return;
}
And then use like a regular function, should re index the array
you can also use native functions such as array_values which returns the values of an array into a single dimension array, causing it to be re indexed .

Categories