我需要在 Drupal 应用程序中重构多维数组。
这是完整图片:
http://imageshack.us/photo/my-images/405/array.png/
field_new => [...]
必须变成'value' => '11111'
您的输入数组:
...
0{
data{
nid => 1
vid => 1
type => article
field_new{
und{
0{
value => 11111
}
}
}
}
}
1{
data{
nid => 2
vid => 2
type => article
field_new{
und{
0{
value => 33333
}
}
}
}
}
您想要的阵列:
...
0{
data{
nid => 1
vid => 1
type => article
value => 11111
}
}
1{
data{
nid => 2
vid => 2
type => article
value => 33333
}
}
Unset(http://php.net/manual/en/function.unset.php)是我们的朋友。
unset() 销毁指定的变量。
使用此代码:
<?php
$loop_count = count($input_arr);
for($i=0;$i<loop_count;$i++){
//copy the value to the desired place
$input_arr[$i]["data"]["value"] = $input_arr[$i]["data"]["field_new"]["und"][0]["value"];
//delete the unwanted portion
unset($input_arr[$i]["data"]["field_new"]);
} // for loops ENDS
?>
假设:
请添加您用来生成/获取此数组的代码,我们可以为您提供更具体的答案。
我建议直接使用数组解构语法修改输入数组,将
data
子数组隔离为引用变量,然后将 value
声明为 field_new
的最深标量值。
代码:(演示)
foreach ($array as ['data' => &$data]) {
$data['value'] = $data['field_new']['und'][0]['value'];
unset($data['field_new']);
}
var_export($array);