Page 1 of 1

get the maximum value from an element in a multidimensional

Posted: Fri Feb 19, 2010 3:51 pm
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>

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

Posted: Fri Feb 19, 2010 4:49 pm
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'];