Jump to content

Array Question


ashzor

Recommended Posts

Here is an example of the array I am using

 

<?php
$users = array( 
		   array( Name => "Ash", 
                           Age => 21,
                           Sex => "Male"
                    ),
                           array( Name => "Kerri", 
                           Age => 18,
                           Sex => "Female"
                    ),
                           array( Name => "Mike", 
                           Age => 15,
                           Sex => "Male"
                    )
             );

for ($row = 0; $row < 3; $row++)
{
    echo $users[$row]["Name"] . " is " . $users[$row]["Age"] . " years old, and is " . $users[$row]["Sex"];
    echo "<br />";
}

?>

 

It returns

Ash is 21 years old, and is Male

Kerri is 18 years old, and is Female

Mike is 15 years old, and is Male

 

My question is, is it possible to identify the arrays inside the $users array as names rather then by row number?

 

For example when I do the echo for row 0 which is ash,21,male rather then echo the row number maybe to do something like

echo $users[$Ash]["Name"] . " is " . $users[$Ash]["Age"] . " years old, and is " . $users[$Ash]["Sex"];

 

instead of using Row Numbers?

 

New to php so not quite sure how to explain what I want to do but basicaly I want to name the arrays inside the $users array rather then use row number.

Link to comment
https://forums.phpfreaks.com/topic/189917-array-question/
Share on other sites

$users = array (

    "ash"  => array("name" => "Ash", "age" => "21", "sex" => "Male"),

    "kerri" => array("name" => "Kerri", "age" => "18", "sex" => "Female"),

    "mike"  => array("name" => "Mike", "age" => "15", "sex" => "Never"),

 

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/189917-array-question/#findComment-1002115
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.