开发者

array for loop syntax

开发者 https://www.devze.com 2023-02-23 11:24 出处:网络
If I have: $data = array( \'id01\' 开发者_JAVA技巧=> array( \'firstName\' => \'Eric\', \'lastName\' => \'Smith\',

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消