PHP 实现键值对的关联数组
关联数组将其索引作为字符串,以便您可以在键和值之间建立牢固的关联。关联数组具有分配给它们的名称键。
让我们看一个例子-
$arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115");
上面,我们可以看到数组中的键和值对。
要在PHP中实现关联数组,代码如下-
示例
<?php $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" ); echo "Value 1 = " .reset($arr); ?>
输出结果
这将产生以下输出-
Value 1 = 150
示例
现在让我们来看另一个示例-
<?php $salaries = array("jack" => 2000, "qadir" => 1000); echo "Salary of jack is ". $salaries['jack'] . "\n"; $salaries['jack'] = "high"; $salaries['tom'] = "low"; echo "Salary of jack is ". $salaries['jack'] . "\n"; ?>
输出结果
这将产生以下输出-
Salary of jack is 2000 Salary of jack is high