echo "<pre>";
print_r($data);
echo "</pre>";
Array
(
[0] => Array
(
[Deposit] => Array
(
[id] => 63393
[amount] => 200.00
[date] => 2014-06-17 08:40:13
[details] =>
)
[User] => Array
(
[username] => presido490
[group_id] => 1
)
)
)
如何移动[username] =&gt;来自[User]的presido490 =&gt; Array()到[Deposit] =&gt; Array(),我需要输出
Array
(
[0] => Array
(
[Deposit] => Array
(
[id] => 63393
[amount] => 200.00
[date] => 2014-06-17 08:40:13
[username] => presido490
[details] =>
)
[User] => Array
(
[group_id] => 1
)
)
)
答案 0 :(得分:1)
在简单的循环中完成
foreach($dataas $key => $element) {
$data[$key]['Deposit']['username'] = $data[$key]['User']['username'];
unset($data[$key]['User']['username']);
}
如果您愿意,可以在array_walk()或array_map()而不是循环中执行此操作
修改强>
如果您必须在username
和date
之间定位details
(建议您首先不了解如何使用数组数据)。
foreach($dataas $key => $element) {
$data[$key]['Deposit'] = array(
'id' => $data[$key]['Deposit']['id'],
'amount' => $data[$key]['Deposit']['amount'],
'date' => $data[$key]['Deposit']['date'],
'username' => $data[$key]['User']['username'],
'details' => $data[$key]['Deposit']['details'],
);
unset($data[$key]['User']['username']);
}
但除了增加开销
之外,这根本不会对你的代码产生任何影响