How to echo array values and hide some of them?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pano pano
    New Member
    • Nov 2011
    • 18

    How to echo array values and hide some of them?

    I'm really stuck with this. I have this script:


    Code:
    $alpha = array(6=>8, 1=>5, 2=>9);
    ksort($alpha);
    foreach ($alpha as $key => $val)	
    	{echo "$val<br />\n";}

    result is

    5
    9
    8

    It works perfectly, but i want to be able to reduce the number of the results.
    Instead of all 3 values, i want to let it sort the array values but echo just the first 2 values. Is it possible?
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    your question is not clear but if you meant top 2 then just use a counter to keep track
    Code:
    $i=0;
    $c=2;
    foreach($array as $val)
    {
    echo $val ;
    $i++;
    if($i==$c)
    break ;
    }

    Comment

    • pano pano
      New Member
      • Nov 2011
      • 18

      #3
      Yes, that's what i mean.
      Instead of
      5
      9
      8

      get

      5
      9

      thanks

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        @pano, it sounds like you're still waiting on an answer but johny already answered your question.

        Comment

        • pano pano
          New Member
          • Nov 2011
          • 18

          #5
          Sorry i wasn't clear. It works perfectly.
          thanks again.

          Comment

          Working...