0

我有一个如下数组:

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只是一个$_GETor$_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]这是错误的。

我怎样才能避免这种情况?

有什么建议吗?

4

2 回答 2

0

您需要在循环的每次运行中重新初始化 $vals-array。否则它的值和键会累积......

foreach ($inserts as $insert) {

        $vals = array();

        if (isset($data[$insert['parent']]))
                $vals['owner'] = $insert['owner'];
                $vals[$insert['name']] = $data[$insert['parent']];
                $tables[$insert['table']] = $vals;     
        }
于 2013-07-15T09:06:07.220 回答
0

假设在 foreach 中插入数组与通过 GET/POST 获得的数据相同此外,如果您需要通过给定表连接这些数组值,则可以这样:

$refractored = array(); // refractored data
//insert has table key
foreach($inserts as $insert) 
{
    if(array_key_exist($insert["table"]))
    {
        $refractored[$insert["table"]] = array_merge($refractored[$insert["table"]], $insert);
    } else  {
        $refractored[$insert["table"]] = $insert;
    }
}
于 2013-07-15T09:28:38.437 回答