Php 将数组附加到特定数组元素
我目前有这个Php 将数组附加到特定数组元素,php,arrays,Php,Arrays,我目前有这个数组结构,稍后我会将其转换为JSON: Array ( [response] => Array ( [timestamp] => 1391457939 [crimes] => Array ( [year] => 6-2013 ) ) ) 我想知道是否有一种方法可以在不指
数组
结构,稍后我会将其转换为JSON
:
Array
(
[response] => Array
(
[timestamp] => 1391457939
[crimes] => Array
(
[year] => 6-2013
)
)
)
我想知道是否有一种方法可以在不指定根数组节点的情况下将数组数据附加到选定的数组节点。例如,我想从上面将以下数组
附加到犯罪
数组:
array(
"Robbery" => 123,
"Burglary" => 456
);
所以它应该是这样的:
Array
(
[response] => Array
(
[timestamp] => 1391457939
[crimes] => Array
(
[year] => 6-2013
[Robbery] => 123 //Appended Data
[Burglary] => 456 //Appended Data
)
)
)
功能:
public function addDataToJSONResp($parentArrayName, $arrayData){
//jsonResponse is the main array which currently holds the array data which will be converted into JSON at a later step.
if(isset($this->jsonResponse)){
//Do processing here
//Search for parent array node in jsonResponse.
//append $arrayData to jsonResponse if it finds the parent array element.
}else{
//error
}
}
所使用的功能:
private function addDataToJSONResp($nodeName, &$array, $data) {
foreach ($array as $key => $val) {
if ($key == $nodeName) {
foreach($data as $k => &$v){
$array[$key][$k] = $v;
}
}else if(is_array($val)){
$this->addDataToJSONResp($nodeName, $val, $data);
}
}
}
可以将对子数组的引用存储在变量中:
$root = array(
'response' => array(
'timestamp' => 1391457939,
'crimes' => array(
'year' => '6-2013'
)
)
);
$crimes = &$root['response']['crimes'];
现在,无论何时更改$crimes
,它都将反映在$root
中
要附加数组,请执行以下操作:
array(
"Robbery" => 123,
"Burglary" => 456
);
然后你可以做:
$crimes = array_merge($crimes, array(
"Robbery" => 123,
"Burglary" => 456
));
或者:
$crimes['Robbery'] = 123;
$crimes['Burglary'] = 456;
输出
Array
(
[response] => Array
(
[timestamp] => 1391457939
[crimes] => Array
(
[year] => 6-2013
[Robbery] => 123
[Burglary] => 456
)
)
)
这是一个由ideone.com主持的网站。这就是你想要实现的目标吗
<?php
/**
* We're passing the $inputArray in as reference
* since we want the passed array to be changed
* right away
*
* $dataArray is the array we want to append
*
* $node, is the array key of the array we want our data
* to append on
**/
function pushToNode(&$inputArray, $dataArray, $node)
{
// Traverse our Input array
foreach($inputArray AS $key => &$value)
{
// If the key of the current iteration
// matches the node we want to append
// our data on, we're done.
if($key === $node){
// We just iterate trough our data array...
foreach($dataArray AS $k => &$v)
{
// and append the keys and the values
// to the array we wanted to.
$inputArray[$key][$k] = $v;
}
return;
}
// Since you don't want to specify the root node
// our function needs to be recursive
if(is_array($value))
{
pushToNode($value, $dataArray, $node);
}
}
}
$myArray = [
'response' => [
'timestamp' => 1293845329,
'crimes' => [
'year' => '1995'
]
]
];
$crimes = [
'robbery' => 'I robbed someone, wow!',
'burglary' => 'Oh look. Catch me if you can.'
];
pushToNode($myArray, $crimes, 'crimes');
echo "<pre>";
print_r($myArray);
echo "</pre>";
重要的代码是注释的,但基本上我们是递归地遍历输入数组。如果我们在要附加数据的节点上,我们会附加数据并返回。我们的输入数组作为引用传入,因此数组本身将被更改。您必须以某种方式指定结构。决定将数据附加到何处的业务规则是什么?我将有一个函数,允许用户指定要将数据附加到的数组的父名称。@user3166216向我们显示您现在提到的此函数的签名…我试图使用array_walk来避免内部foreach,但是把这里的参考资料搞砸了。如果有人能给我一个如何“更干净”的提示,我将不胜感激代码>不会将值附加到数组中。您是如何使用该函数的?我对它进行了充分的测试,它工作得完美无缺。我将添加输出。使用我使用的函数更新了我的问题。这是最下面的。我终于发现了错误。我错过了第一个
foreach
$value
的&
。谢谢
Array
(
[response] => Array
(
[timestamp] => 1293845329
[crimes] => Array
(
[year] => 1995
[robbery] => I robbed someone, wow!
[burglary] => Oh look. Catch me if you can.
)
)
)