Solved

PHP: Sort array by value, keep keys

Posted on 2011-09-20
2
423 Views
Last Modified: 2012-05-12
How can I sort my array by values, while maintaining keys?

My code below sorts by the keys, not the values:
<pre><?php

$zzz=array();

$zzz['b']='Banana';
$zzz['z']='Cherry';
$zzz['m']='Apple';
$zzz['a']='Durian';

asort($zzz);

$zzz = array_merge(array('a'=>'Apple'),$zzz);

print_r($zzz);

?>
</pre>

Open in new window


I want this:

Array
(
    [m] => Apple
    [b] => Banana
    [z] => Cherry
    [a] => Durian
 )

Open in new window

0
Comment
Question by:hankknight
  • 2
2 Comments
 
LVL 4

Accepted Solution

by:
MattJellings earned 500 total points
ID: 36569473
Hi hankknight,

I've had a quick look and from what I can see the asort() function is the correct one to use, but I belive the array_merge line you are using afterwards is affecting the order of your array.

For example, try performing another asort() after the array_merge() as follows:

$zzz=array(); 
 
$zzz['b']='Banana'; 
$zzz['z']='Cherry'; 
$zzz['m']='Apple'; 
$zzz['a']='Durian'; 
 
asort($zzz); 
 
$zzz = array_merge(array('a'=>'Apple'),$zzz); 
 
echo '<pre>'; print_r($zzz); echo '</pre>'; 

asort($zzz);

echo '<pre>'; print_r($zzz); echo '</pre>';

Open in new window


This gives the following (note the second print_r statement outputs the array in your desired order.

Array
(
    [a] => Durian
    [m] => Apple
    [b] => Banana
    [z] => Cherry
)
Array
(
    [m] => Apple
    [b] => Banana
    [z] => Cherry
    [a] => Durian
)

Open in new window


If this is in fact the case then I would suggest performing a single asort() after your have finished running any required array_merge() functions.

I hope this helps with your problem.

Cheers

Matt
0
 
LVL 4

Expert Comment

by:MattJellings
ID: 36569516
Just to provide a bit more information for you, the PHP manual page for array_merge() states the following:

"Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one."

"If the input arrays have the same string keys, then the later value for that key will overwrite the previous one."

This would explain the problem.

What I believe is happening is you are first creating the array $zzz and then you are appending this array onto your array('a'=>'Apple') created on line 10.

This is first creating a new array, with a single element who's key is 'a' and value is 'Apple'.  The array_merge() is then appending the existing $zzz array onto the end.   Leaving 'a'=>'Apple' as the first element in the array.  Subsequently, as the documentation states, because your $zzz array has an existing key 'a' who's value is 'Durian' this is overwriting 'a'.

My apologies, I'm struggling to explain it however hopefully it makes some sense to you.

Cheers

Matt
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question