我的数组是这样的:
$arr = array('chelsea.jpg', 'arsenal.jpg');
如果我运行: echo '<pre>';print_r($arr);echo '</pre>';
结果 :
Array
(
[0] => chelsea.jpg
[1] => arsenal.jpg
)
我想换钥匙。所以结果是这样的:
Array
(
[cover1] => chelsea.jpg
[cover2] => arsenal.jpg
)
我该怎么做?
你可以使用经典 foreach
$arr = array('chelsea.jpg', 'arsenal.jpg');
$final = array();
foreach( $arr as $key => $val ) {
//Notice that $key + 1 -> because the first key of a simple array is 0
//You are assigning here the NEW key inside []
$final[ "cover" . ( $key + 1 ) ] = $val;
}
echo "<pre>";
print_r( $final );
echo "</pre>";
这将导致
Array
(
[cover1] => chelsea.jpg
[cover2] => arsenal.jpg
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句