array for loop syntax
If I have:
$data = array(
'id01' 开发者_JAVA技巧=> array(
'firstName' => 'Eric',
'lastName' => 'Smith',
),
'id02' => array(
'firstName' => 'John',
'lastName' => 'Turner',
),
);
foreach ( $data as $key){
print "$key[firstName]<br>";
echo $key[0];
}
The $key[0]
part is not working...basically i'm trying to output id01, then id02, basically the id part of the array that the forloop is processing...
Any ideas on the correct syntax?
What you need is
foreach ($data as $key => $val){
print "$val[firstName]<br>"; //changed to $val
echo $key; //prints id01, id02
}
There is no key for 0
, just first and last name - you need to do this
foreach ($data as $key => $value)
{
echo "Key is " . $key . ", value of firstName
is " . $value["firstName"] . "<br />";
}
something like this?
foreach ( $data as $key=>$value){
print "$value[firstName]<br>";
echo $key.'<br />';
}
Try:
foreach ( $data as $key=>$value)
精彩评论