如何在PHP中通过整数索引访问关联数组?
要在PHP中通过整数索引访问关联数组,代码如下-
示例
<?php$arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110");
$keys = array_keys( $arr );
echo "Array key and value...\n";
for($x = 0; $x < sizeof($arr); $x++ ) {
echo "key: ". $keys[$x] . ", value: ". $arr[$keys[$x]] . "\n";
}
?>
输出结果
Array key and value...key: p, value: 150
key: q, value: 100
key: r, value: 120
key: s, value: 110
示例
现在让我们来看另一个例子-
<?php$arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110");
$keys = array_keys( $arr );
echo "Array key and value...\n";
for($x = 0; $x < sizeof($arr); $x++ ) {
echo "key: ". $keys[$x] . ", value: ". $arr[$keys[$x]] . "\n";
}
$arr[$keys[2]] = "20";
$arr[$keys[3]] = "10";
echo "\nUpdated Array key and value...\n";
for($x = 0; $x < sizeof($arr); $x++ ) {
echo "key: ". $keys[$x] . ", value: ". $arr[$keys[$x]] . "\n";
}
?>
输出结果
Array key and value...key: p, value: 150
key: q, value: 100
key: r, value: 120
key: s, value: 110
Updated Array key and value...
key: p, value: 150
key: q, value: 100
key: r, value: 20
key: s, value: 10
以上是 如何在PHP中通过整数索引访问关联数组? 的全部内容, 来源链接: utcz.com/z/326554.html