Re: retrieve top n entries from an associative array



Below $a is an associative array. It is then sorted by value using asort() and then its top ten elements are extracted using array_slice().
I think this is what you want?

Myron

<?php

$a = array( 'abc1' => 'apple',
'abc3' => 'angle',
'def' => 'dog' ,
'e' => 'egg',
'xyz' => 'word',
'm' => 'man',
'n' => 'nose',
'p' => 'pan',
'l' => 'lock',
'h' => 'head',
'c' => 'cook',
'g' => 'garden',
'f' => 'food'

);

asort($a);
echo "Associative Array Sorted By Value:\n";
print_r($a);

echo "Top ten elements:\n";
$top_ten = array_slice ( $a, 0, 10);
print_r ($top_ten);


/* Result:
Associative Array Sorted By Value:
Array
(
[abc3] => angle
[abc1] => apple
[c] => cook
[def] => dog
[e] => egg
[f] => food
[g] => garden
[h] => head
[l] => lock
[m] => man
[n] => nose
[p] => pan
[xyz] => word
)
Top ten elements:
Array
(
[abc3] => angle
[abc1] => apple
[c] => cook
[def] => dog
[e] => egg
[f] => food
[g] => garden
[h] => head
[l] => lock
[m] => man
)
*/
?>




duzhidian@xxxxxxxxx wrote:
Thanks for the replies. But both programs only meaningful for the
indexed array, not associative array.

I have no idea how to retrieve associative array according to the
length. Can you explain it?

--
Myron Turner wrote:
array array_slice ( array array, int offset [, int length])

<?php
$top_ten = array_slice ( $sorted, 0 , 10);

$a= array (1,2,3,4,5,6,7,8,9,10,11,12);
$top_ten = array_slice ( $a, 0, 10);
print_r ($top_ten);
?>

/* Result:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
*/


duzhidian@xxxxxxxxx wrote:
Hello,

After sorting an associative array by value, how can I retrieve top n
entries from it?

Thanks.

Zhidian Du


--

_____________________
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/



--

_____________________
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/
.



Relevant Pages

  • Re: [PHP] Urgent: Way to XML-parse data with tags?
    ... Myron Turner wrote: ... if ($insideitem) { ... You'll get these from the attributes array, which is an associative array: ... // speed up the parsing since all the values have been found ...
    (php.general)
  • Re: retrieve top n entries from an associative array
    ... indexed array, not associative array. ... I have no idea how to retrieve associative array according to the ... Myron Turner wrote: ...
    (php.general)
  • Re: Save an Image to a file
    ... associative array with key-value pairs, and as far as I know, there is ... might be able to get away with using PHP. ... binmode TEST; ...
    (comp.lang.java.programmer)
  • RE: [PHP] finding the index name of an associative array
    ... [PHP] finding the index name of an associative array ... Is there an iterative way to find out the array index values ('a', ... Checked by AVG Free Edition. ...
    (php.general)
  • Re: I almost get what youre saying...
    ... but I'm so new to PHP that I need just a "little more" pointing.... ... From reading the docs, I understand that I need to pull from an associative array, and you are correct that "$venuecity = $attrs". ... function startElement($parser, $name, $attrs) { ... if ($insideitem) { ...
    (php.general)