get the maximum value from an element in a multidimensional

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
BrettCarr
Forum Newbie
Posts: 22
Joined: Fri Feb 12, 2010 6:45 pm

get the maximum value from an element in a multidimensional

Post by BrettCarr »

I'm trying to select the maximum value for a particular key in a multidimensional array. I'm having trouble "getting to" the key in question...
so here my post

Code: Select all

 
<?php
    session_start();
        if(isset($_POST['fname']))
        {
        if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['phone']))
            {
         $_SESSION['formArray'][] = array("fname" => $_POST['fname'],
                                             "lname" => $_POST['lname'],
                                             "phone" =>  $_POST['phone']);
            }
            else
            {
            print "Error: Not enough form data";
            }
        }
        
        for($i = 0; $i <= count($_SESSION['formArray']); $i++)
        {
        print $_SESSION['formArray'][$i]['fname'] . " " . $_SESSION['formArray'][$i]['lname'] . " " . $_SESSION['formArray'][$i]['phone'] . "<br />"; 
        }
        
       /* Just echoing out to see weather i can access them*/
        for ( $k = 0; $k <= count($_SESSION['formArray']); $k++ )
        {
            
            echo "{$_SESSION['formArray'][$k]['phone']}"."<br/>";
        
        } 
/*ok i would like to see only the highest number from the phone key, just the highest number*/
        $out = array(); 
foreach($_SESSION['formArray'] as $obj) 
{ 
    $out[] = $obj->phone; 
} 
echo "this would be the highest number" . max($out); 
 
        
 
 
        
        
        //session_destroy();
        
 
?>
 
 
So as you can see Its not outputting anything

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bretts Array</title>
</head>
    <body>
        <form name="info" action="" method="post">
        first name: <input type="text" name="fname" value=""><br/>
        last name:<input type="text" name="lname" /><br />
        phone number:<input type="text" name="phone" /><br />
        <input type="submit"/>
        </form>
    </body>
</html>
Last edited by BrettCarr on Fri Feb 19, 2010 4:56 pm, edited 1 time in total.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: get the maximum value from an element in a multidimensional

Post by Weiry »

BrettCarr wrote:

Code: Select all

foreach($_SESSION['formArray'] as $obj)
{
    $out[] = $obj->phone;
}
$obj isn't a class object, its an array try:

Code: Select all

$out[] =  $obj['phone'];
Post Reply