Page 1 of 1

help with arrays

Posted: Tue Mar 11, 2008 5:00 pm
by gammaman
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Can I do the following, which finds the nth largest value from an array, using a 2d array, that finds the nth largest from each.

Here is my code using the 1d array, please modify for the 2d array. Any help is appriciated.

Code: Select all

 
<?php
 
 function nthlargest($array,$n)
 {
   rsort($array);
   return $array[$n-1];
 
 }
 
 $my_array=array(1,2,3,4,5);
 $x=3;
 $value=size($my_array,$x);
 echo "$value";
?>
 

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: help with arrays

Posted: Tue Mar 11, 2008 5:19 pm
by Christopher
Does the code work? Did you try to call nthlargest($my_array,$x) ?

Re: help with arrays

Posted: Tue Mar 11, 2008 5:23 pm
by gammaman
Yes this works fine, but it uses a one dimensional array, I want to do the same thing using a
two dimensional array.

Re: help with arrays

Posted: Tue Mar 11, 2008 5:28 pm
by gammaman
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


want to do something like this

Code: Select all

 
<?php
 
 $mine=array(array(1,2,3,4,5),
             array(6,7,8,9,10));
 
 $x=5;
 $answer=nthlargest($mine,$x);
 
 echo "$answer";
 
 function nthlargest($array,$n)
 {
 
  foreach($array as $itm =>$indv)
  {
    rsort($indv);
   foreach($indv as $sum)
   {
    return $sum[$n-1];
   }
 
  }
 }
 

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.