我有像
这样的数组$arr = array(1 => array('name' => 'abc', 'age' =>25), 2 => array('name' => 'xyz', 'age' =>28))
我想在名称值中加上前缀Mr.所以数组将成为
$arr = array (1 => array('name' => 'Mr. abc', 'age' =>25), 2 => array('name' => 'Mr. xyz', 'age' =>28))
在php中,我可以这样做吗?
答案 0 :(得分:1)
你可以操纵"名称"索引值根据您的要求,如:
Not Found
The requested document was not found on this server.
点击此链接至see result
答案 1 :(得分:1)
假设您的数组名称是$ array ...
foreach($array as $id=>$el){
$array[$id]['name']='Mr '.$el['name'];
}
答案 2 :(得分:1)
请尝试以下代码。它可能对你有帮助。
$array = array(1 => array('name' => 'abc', 'age' =>25), 2 => array('name' => 'xyz', 'age' =>28));
foreach($array as &$element)
{
$element['name']= 'Mr. '.$element['name'];
}
print_r($array);
答案 3 :(得分:1)
使用 array_map - 将回调应用于给定数组的元素。
示例 - 强>
<?php
function funct($ar) {
$ar['name'] = "Mr. ".$ar['name'];
return $ar;
}
$array = array(1 => array('name' => 'abc', 'age' =>25), 2 => array('name' => 'xyz', 'age' =>28));
$array = array_map('funct', $array);
pr($array);
?>
答案 4 :(得分:1)
试试这是代码
<?php
$array= Array (1 => array('name' => 'abc', 'age' =>25), 2 => array('name' => 'xyz', 'age' =>28));
foreach($array as &$value) {
$value['name'] = 'MR. '.$value['name'];
}
print_r($array);
?>
/* output
Array ( [1] => Array ( [name] => MR. abc [age] => 25 ) [2] => Array ( [name] => MR. xyz [age] => 28 ) )
*/