Three forms of php array output detail
- 2020-06-03 06:09:04
- OfStack
$bbbb=array("11"=>"aaa","22"=>"bbb");
// Output value only value Can't output key
foreach($bbbb as $color)
{
echo $color;
}
//value with key Can be output
foreach($bbbb as $key=>$value)
{
echo $key."=>".$value;
}
//value with key Can be output
while($color=each($bbbb)){
echo $color['key'];
}
or
while(list($key,$value)=each($bbbb)){
echo "$key : $value<br>";
}
Direct access to array elements:
<?php
$arr=array('w'=>'wen','j'=>'jian','b'=>'bao');
echo($arr['w']),'<br/>';// Play a role
echo($arr[w]),'<br/>';// Play a role
echo($arr[0]),'<br/>';// It doesn't work, somehow...?
echo($arr['j']),'<br/>';// Play a role
echo($arr[j]),'<br/>';// Play a role
echo($arr[1]),'<br/>';// It doesn't work, somehow...?
echo($arr['b']),'<br/>';// Play a role
echo($arr[b]),'<br/>';// Play a role
echo($arr[2]),'<br/>';// It doesn't work, somehow...?
?>
Output:
wen
wen
jian
jian
bao
bao
Suspect:
Accessing associative array elements,
1. The key in [] can be accessed without quotation marks (" ").
2, array index access is not working??
<?php
$arr1=array('wen','jian','bao');
echo $arr1[0],'<br/>',$arr1[1],'<br/>',$arr1[2];
?>
Output:
wen
jian
bao