PHP mysql查询结果到多维数组

时间:2015-09-02 11:20:26

标签: php mysql arrays multidimensional-array

while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {    
$basicinfo[] = array('schlid' => $selected_row['schlid'], 'name' => $selected_row['name'], 'class' => $selected_row['class']);
}

上面的数组制作将产生一个数组,该数组具有多个值,数组将如下所示

Array
(
    [0] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => Math 
        )

    [1] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => English 
        )

    [2] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => Math 
        )

    [3] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => English 
        )

    [4] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => Math 
        )

    [5] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => English 
        )

    [6] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => Math 
        )

    [7] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => English 
        )

    [8] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => Math 
        )

    [9] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => English 
        )

    [10] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => Math 
        )

    [11] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => English 
        )

)

但我可以通过以下方式删除相同的值

$input = array_map("unserialize", array_unique(array_map("serialize", $basicinfo)));
$new_data = array_values($input); 

我会在下面得到这个

Array
(
    [0] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => Math 
        )

    [1] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [class] => English 
        )
)

现在我所做的是创建一个与此

相同的查询结果的多维数组
    while ($selected_row = $stmt - > fetch(PDO::FETCH_ASSOC)) {
         $tmp_key = $selected_row['schlid']; //adding a temp key
         $basicinfo[$tmp_key]['name'] = $selected_row['name'];
         $basicinfo[$tmp_key]['schlid'] = $selected_row['schlid'];
         $sudky = (isset($basicinfo[$tmp_key]['Class'])) ? count($basicinfo[$tmp_key]['Class']) : 0;
         $basicinfo[$tmp_key]['Class'][$sudky]['class'] = $selected_row['class'];
    }

从上面我得到的结果是

Array
(
    [015-08-0011-000-01] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [Class] => Array
                (
                    [0] => Array
                        (
                            [class] => Math 
                        )

                    [1] => Array
                        (
                            [class] => English 
                        )

                    [2] => Array
                        (
                            [class] => Math 
                        )

                    [3] => Array
                        (
                            [class] => English 
                        )

                    [4] => Array
                        (
                            [class] => Math 
                        )

                    [5] => Array
                        (
                            [class] => English 
                        )

                    [6] => Array
                        (
                            [class] => Math 
                        )

                    [7] => Array
                        (
                            [class] => English 
                        )

                    [8] => Array
                        (
                            [class] => Math 
                        )

                    [9] => Array
                        (
                            [class] => English 
                        )

                    [10] => Array
                        (
                            [class] => Math 
                        )

                    [11] => Array
                        (
                            [class] => English 
                        )

                )

        )

)

我想要的是

Array
(
    [015-08-0011-000-01] => Array
        (
            [schlid] => 015-08-0011-000-01
            [name] => John
            [Class] => Array
                (
                    [0] => Array
                        (
                            [class] => Math 
                        )

                    [1] => Array
                        (
                            [class] => English 
                        )
                )

        )

)

删除多维aray中的重复元素。 上面的结果来自PHP中的print_r(),如果这是相关的

1 个答案:

答案 0 :(得分:0)

您可以使用for 循环检查之前指定学生班级,或者在输出后处理包含重复班级的数组使用array_unique

$basicinfo[$tmp_key]['Class'][0] = array_unique($basicinfo[$tmp_key]['Class'][0]); //<-The array that you don't want duplicates in. i modify the formating