retrieve array subset in random order

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kevincw01

    retrieve array subset in random order

    Anyone have a clever way to retrieve for example, items 0-29 from an
    array of size N>29, in random order? The catch is that I dont want to
    print any items more than once and I dont want to miss any.

    This is for a tag cloud where I have sorted them by popularity but now
    I need to display only the top 30. I do this now but they are in
    order of popularity and its not really a 'cloud'. If I could retrieve
    the top 30 in some random order, it would look better.

    Thanks

  • Rik

    #2
    Re: retrieve array subset in random order

    kevincw01 <kevin@netkev.c omwrote:
    Anyone have a clever way to retrieve for example, items 0-29 from an
    array of size N>29, in random order? The catch is that I dont want to
    print any items more than once and I dont want to miss any.
    >
    This is for a tag cloud where I have sorted them by popularity but now
    I need to display only the top 30. I do this now but they are in
    order of popularity and its not really a 'cloud'. If I could retrieve
    the top 30 in some random order, it would look better.
    $array = array(....somet hing..);
    $top = array_splice(0, 30,$array);
    shuffle($top);

    Where are you getting your data from? Seems it might be better to not have
    this in the array to begin with.
    --
    Rik Wasmus
    Posted on Usenet, not any forum you might see this in.
    Ask Smart Questions: http://tinyurl.com/anel

    Comment

    • kevincw01

      #3
      Re: retrieve array subset in random order

      On Mar 11, 12:20 am, Rik <luiheidsgoe... @hotmail.comwro te:
      kevincw01 <k...@netkev.co mwrote:
      Anyone have a clever way to retrieve for example, items 0-29 from an
      array of size N>29, in random order? The catch is that I dont want to
      print any items more than once and I dont want to miss any.
      >
      This is for a tag cloud where I have sorted them by popularity but now
      I need to display only the top 30. I do this now but they are in
      order of popularity and its not really a 'cloud'. If I could retrieve
      the top 30 in some random order, it would look better.
      >
      $array = array(....somet hing..);
      $top = array_splice(0, 30,$array);
      shuffle($top);
      >
      Where are you getting your data from? Seems it might be better to not have
      this in the array to begin with.
      --
      Rik Wasmus
      Posted on Usenet, not any forum you might see this in.
      Ask Smart Questions:http://tinyurl.com/anel
      Interesting, I'm getting the famous array reference error with that
      code(first line below):
      <?php $top = array_splice(0, $tags2show,$tag s); ?>
      <?php shuffle($top); ?>

      Fatal Error: Only variables can be passed by reference...

      The fix should be to store function array results in a temp variable
      but I don't see where I'm not doing that?

      Comment

      • kevincw01

        #4
        Re: retrieve array subset in random order

        On Mar 11, 12:38 am, "kevincw01" <k...@netkev.co mwrote:
        On Mar 11, 12:20 am, Rik <luiheidsgoe... @hotmail.comwro te:
        >
        >
        >
        kevincw01 <k...@netkev.co mwrote:
        Anyone have a clever way to retrieve for example, items 0-29 from an
        array of size N>29, in random order? The catch is that I dont want to
        print any items more than once and I dont want to miss any.
        >
        This is for a tag cloud where I have sorted them by popularity but now
        I need to display only the top 30. I do this now but they are in
        order of popularity and its not really a 'cloud'. If I could retrieve
        the top 30 in some random order, it would look better.
        >
        $array = array(....somet hing..);
        $top = array_splice(0, 30,$array);
        shuffle($top);
        >
        Where are you getting your data from? Seems it might be better to not have
        this in the array to begin with.
        --
        Rik Wasmus
        Posted on Usenet, not any forum you might see this in.
        Ask Smart Questions:http://tinyurl.com/anel
        >
        Interesting, I'm getting the famous array reference error with that
        code(first line below):
        <?php $top = array_splice(0, $tags2show,$tag s); ?>
        <?php shuffle($top); ?>
        >
        Fatal Error: Only variables can be passed by reference...
        >
        The fix should be to store function array results in a temp variable
        but I don't see where I'm not doing that?
        Wait I see the problem. That's the incorrect array_splice syntax.
        Correct is array_splice($a rray,first_inde x,last_index). Works well
        now.

        Comment

        • Rik

          #5
          Re: retrieve array subset in random order

          kevincw01 <kevin@netkev.c omwrote:
          Wait I see the problem. That's the incorrect array_splice syntax.
          Correct is array_splice($a rray,first_inde x,last_index). Works well
          now.
          Oops, still early for a sunday morning :P

          --
          Rik Wasmus
          Posted on Usenet, not any forum you might see this in.
          Ask Smart Questions: http://tinyurl.com/anel

          Comment

          Working...