将数组从MySQL JOIN(带有重复项)转换为嵌套的关联数组

时间:2014-03-25 02:49:12

标签: php mysql arrays

我从MySQL JOIN获得了以下结果:

+------------+----------------+-----------+---------+
| session_id | session_name   | user_name | user_id |
+------------+----------------+-----------+---------+
|         13 | Test session 1 | User 1    |      29 |
|         13 | Test session 1 | User 2    |      30 |
|         14 | test session 2 | User 1    |      31 |
|         14 | test session 2 | User 2    |      32 |
|         14 | test session 2 | User 3    |      33 |
|         14 | test session 2 | User 4    |      34 |
+------------+----------------+-----------+---------+

...产生这个PHP数组:

Array
(
    [0] => Array
        (
            [session_id] => 13
            [session_name] => Test session 1
            [user_name] => User 1
            [user_id] => 29
        )

    [1] => Array
        (
            [session_id] => 13
            [session_name] => Test session 1
            [user_name] => User 2
            [user_id] => 30
        )

    [2] => Array
        (
            [session_id] => 14
            [session_name] => test session 2
            [user_name] => User 1
            [user_id] => 31
        )

    [3] => Array
        (
            [session_id] => 14
            [session_name] => test session 2
            [user_name] => User 2
            [user_id] => 32
        )

    [4] => Array
        (
            [session_id] => 14
            [session_name] => test session 2
            [user_name] => User 3
            [user_id] => 33
        )

    [5] => Array
        (
            [session_id] => 14
            [session_name] => test session 2
            [user_name] => User 4
            [user_id] => 34
        )
)

如何通过过滤session_id重复项将其处理到以下嵌套数组中?

Array
(
    [0] => Array
        (
            [session_id] => 13
            [session_name] => Test session 1
            [user] => Array
                (
                    [0] => Array
                        (
                            [user_name] => User 1
                            [user_id] => 29
                        )
                    [1] => Array
                        (
                            [user_name] => User 2
                            [user_id] => 30
                        )
                )
        )

    [1] => Array
        (
            [session_id] => 14
            [session_name] => test session 2
            [user] => Array
                (
                    [0] => Array
                        (
                            [user_name] => User 1
                            [user_id] => 31
                        )
                    [1] => Array
                        (
                            [user_name] => User 2
                            [user_id] => 32
                        )
                    [2] => Array
                        (
                            [user_name] => User 3
                            [user_id] => 33
                        )
                    [3] => Array
                        (
                            [user_name] => User 4
                            [user_id] => 34
                        )
                )
        )
)

1 个答案:

答案 0 :(得分:1)

尝试:

/* The first foreach stores the number of times each session_id is repeated */
$session_ids = array();
foreach($array as $k=>$v){

    if(!array_key_exists($v['session_id'], $session_ids)){
        $session_ids[$v['session_id']] = 1;
    }
    else
    {
        $session_ids[$v['session_id']] += 1;
    }
}


/*The second foreach will store each session_id once and loop through to
the original array to find user details of that session_id and store them
as nested arrays*/
$new_array = array();
$counter = 0;
foreach($session_ids as $k=>$v){
    $new_array[$counter]['session_id'] = $k;
    foreach($array as $k1=>$v1){
        if($v1['session_id'] == $k)
        {
            $new_array[$counter]['session_name'] = $v1['session_name'];
            $new_array[$counter]['user'][] = array('user_name'=>$v1['user_name'], 'user_id'=>$v1['user_id']);
        }
    }

    $counter++;
}

<强> Demo