LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP Accesing variables within an array (https://www.linuxquestions.org/questions/programming-9/php-accesing-variables-within-an-array-872029/)

LionOfGod 03-30-2011 09:04 PM

PHP Accesing variables within an array
 
Hello,
Im pretty new to PHP so excuse my dumbness :p
I've searched this up in quite a few places and cant find anything : (
Basically, I've made an array, within it are 3 more arrays
Their are two values in each array, 'Name' and 'Age'
Basically Im using a While statement to try and cycle through the 'age' value of my array and state whether or not the person is eligible.
(Im actually just learning so I'm doing this to just test myself)
Here is my code
Code:

<?php
$people =array(
array('name' => 'Bob', 'age' => 15 ),
array('name' => 'Jhon' , 'age' => 10),
array('name' => 'Sue' , 'age' => 7));
//($value =& $people[0,1,2][age] ); Here was an attempt to try and create a reference to age, i tried it many different ways
while($age < 10) {
echo ('$name, is elligible')
        ;
}

?>

Okay, so I tried to do this through two ways, one way i tried to create a reference to age, then evaluate it, without that line of code their would be no reference, which way is correct, why wont it work?
Im using Xammp, so when i launch local host I get an undefined variable error
What To Do?

jlinkels 03-30-2011 09:23 PM

PHP Code:

foreach ($people as $person) {
  if (
$person['age'] < 10){
    do 
something
  
}


jlinkels

LionOfGod 03-30-2011 09:28 PM

Quote:

Originally Posted by jlinkels (Post 4309116)
PHP Code:

foreach ($people as $person) {
  if (
$person['age'] < 10){
    do 
something
  
}


jlinkels

TyVM : D
BUT...
Sorry, i didnt state this before, how do i refer to the certain persons name?
As in, if the person is "elligible" how do i refer to them by name in an echo statement?

jlinkels 03-31-2011 05:43 AM

PHP Code:

foreach ($people as $person) { 
  if (
$person['age'] < 10){ 
     echo 
"{$person['name']} is still young";
  } 


jlinkels


All times are GMT -5. The time now is 04:15 PM.