help with arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

help with arrays

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: help with arrays

Post by Christopher »

Does the code work? Did you try to call nthlargest($my_array,$x) ?
(#10850)
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: help with arrays

Post 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.
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: help with arrays

Post 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.
Post Reply