将项添加到关联数组开头的 PHP 程序
若要将项添加到关联数组的开头,代码如下−
示例
<?php $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" ); echo "Initial Array...
"; print_r($arr); array_unshift($arr,"100"); echo "Updated Array...
"; print_r($arr); ?>
输出
这将生成以下输出−
Initial Array... Array( [p] => 150 [q] => 100 [r] => 120 [s] => 110 [t] => 115 [u] => 103 [v] => 105 [w] => 125 ) Updated Array... Array ( [0] => 100 [p] => 150 [q] => 100 [r] => 120 [s] => 110 [t] => 115 [u] => 103 [v] => 105 [w] => 125 )
示例
我们现在来看另一个示例−
<?php $arr = array( 0=>"Ryan", 1=>"Kevin", 2=>"Jack" ); echo "Initial Array...
"; print_r($arr); array_unshift($arr,"Katie"); echo "Updated Array...
"; print_r($arr); ?>
输出
这将生成以下输出−
Initial Array... Array( [0] => Ryan [1] => Kevin [2] => Jack ) Updated Array... Array( [0] => Katie [1] => Ryan [2] => Kevin [3] => Jack )
广告