我有一个如下数组:
Array
(
[0] => Array
(
[insert_input_id] => 1
[action_id] => 1
[table] => users
[name] => nickname
[parent] => nickname
[owner] =>
)
[1] => Array
(
[insert_input_id] => 2
[action_id] => 1
[table] => users
[name] => email
[parent] => email
[owner] =>
)
[2] => Array
(
[insert_input_id] => 3
[action_id] => 1
[table] => users
[name] => name
[parent] => name
[owner] =>
)
[3] => Array
(
[insert_input_id] => 4
[action_id] => 1
[table] => users
[name] => surname
[parent] => surname
[owner] =>
)
[4] => Array
(
[insert_input_id] => 5
[action_id] => 1
[table] => social
[name] => social_id
[parent] => social_id
[owner] => user_id
)
[5] => Array
(
[insert_input_id] => 6
[action_id] => 1
[table] => social
[name] => social_token
[parent] => social_token
[owner] => user_id
)
[6] => Array
(
[insert_input_id] => 7
[action_id] => 1
[table] => social
[name] => social_type
[parent] => social_type
[owner] => user_id
)
)
正如您从上面的数组中看到的那样,有些具有相同的[table]
值。在这种特定情况下,我们有[table] => social
和[table] => users
。所以我试图根据相同的表重构数组并试图达到以下结果。
Array
(
[users] => Array
(
[owner] =>
[nickname] => giorgio
[email] => giorgia@m.com
[name] => giorgia
[surname] => giorgio
)
[social] => Array
(
[owner] => user_id
[social_id] => 23123
[social_token] =>
[social_type] => facebook
)
)
这就是我对我的 PHP 代码所做的事情:
foreach ($inserts as $insert) {
if (isset($data[$insert['parent']])) {
$vals['owner'] = $insert['owner'];
$vals[$insert['name']] = $data[$insert['parent']];
$tables[$insert['table']] = $vals;
}
}
echo '<pre>'; print_r($tables); echo '</pre>';
$data
只是一个$_GET
or$_POST
请求给出的数据。
关于分配正确的一切都很好$data
。问题是我得到以下数组:
Array
(
[users] => Array
(
[owner] =>
[nickname] => giorgio
[email] => giorgia@m.com
[name] => giorgia
[surname] => giorgio
)
[social] => Array
(
[owner] => user_id
[nickname] => giorgio
[email] => giorgia@m.com
[name] => giorgia
[surname] => giorgio
[social_id] => 23123
[social_token] =>
[social_type] => facebook
)
)
如您所见,[social]
数组还包含数组的元素,[users]
这是错误的。
我怎样才能避免这种情况?
有什么建议吗?