有两种方法来定义关联数组:
第一种方式:
$salary=array("Hema"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
第二种方式:
$salary["Hema"]="550000";
$salary["Vimal"]="250000";
$salary["Ratan"]="200000";
示例文件:arrayassociative1.php
"550000","Vimal"=>"250000","Ratan"=>"200000");
echo "Hema salary: ".$salary["Hema"]."
";
echo "Vimal salary: ".$salary["Vimal"]."
";
echo "Ratan salary: ".$salary["Ratan"]."
";
?>
上面代码执行输出结果 -
Hema salary: 550000 Vimal salary: 250000 Ratan salary: 200000
文件:arrayassociative2.php
";
echo "Vimal salary: ".$salary["Vimal"]."
";
echo "Ratan salary: ".$salary["Ratan"]."
";
?>
上面代码执行输出结果 -
Maxsu salary: 550000 Vimal salary: 250000 Ratan salary: 200000遍历PHP关联数组
使用PHP的foreach循环,我们可以很容易地遍历PHP关联数组的元素。
"550000","Vimal"=>"250000","Ratan"=>"200000");
foreach($salary as $k => $v) {
echo "Key: ".$k." Value: ".$v."
";
}
?>
上面代码执行输出结果 -
Key: Maxsu Value: 550000 Key: Vimal Value: 250000 Key: Ratan Value: 200000
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)