Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to widdle a large array down to the top 3 values?

    I have a large dynamically created array arranged as follows



    [9693] => 2
    [9658] => 41
    [9654] => 42
    [9640] => 24
    [9634] => 4
    [9613] => 29
    [9624] => 433
    [9620] => 5



    The values (2, 41, 42, 24, etc..) are the # of items associated with the key (9693, 9658, 9654, etc...).

    What I want to do is maintain the same order but cut the array down to only the top 3 items in this array based on the values (ie. the number of items associated with that key).

    so that my final array would look like this

    [9658] => 41
    [9654] => 42
    [9624] => 433


    Does anyone have any clue how to tackle this one?

    Thanks!!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Easiest way is to copy the array, sort it, and retrieve the first three.
    PHP Code:
    $aTemp $yourArray;
    asort($aTempSORT_NUMERIC);
    $aOnlyThree array_slice($aTemp03true);
    unset(
    $aTemp); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    darkus (05-16-2010)

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    If you are filling this array by using a MySQL query, let us know (that's where the real solution would be).

    Otherwise, see examples here.
    http://www.developertutorials.com/tu...114/page1.html

    Scroll down for associative array sorting.
    Sort descending, and then pick the first 3 off the top.

    If I missed the point of your post, let us know. I'm not sure if that's what you mean.

  • Users who have thanked mlseim for this post:

    darkus (05-16-2010)

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php
    $array 
    = array(
    '9693' => 2,
    '9658' => 41,
    '9654' => 42,
    '9640' => 24,
    '9634' => 4,
    '9613' => 29,
    '9624' => 433,
    '9620' => 5);

    $foo array_flip($array);
    arsort($fooSORT_NUMERIC );
    $foo array_slice(array_flip($foo), 03true);
    print_r($foo);

    ?>
    Code:
    Array
    (
        [9693] => 2
        [9658] => 41
        [9654] => 42
    )
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    darkus (05-16-2010)

  • #5
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    [9658] => 41
    [9654] => 42
    [9624] => 433
    Think I got it wrong... you want to order by key?? or by val??
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    darkus (05-16-2010)

  • #6
    New Coder
    Join Date
    May 2009
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks guys, I actually got it working by mixing a few suggestions you guys gave!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •