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

The above making of array will result into a array that has multiple value the array will look like this

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 
        )

)

But i am able to remove the same value just by doing this below

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

and i will get this below

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 
        )
)

Now what i did is to make a multidimensional array of the same query result as this

    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'];
    }

From above the result i get is

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 
                        )

                )

        )

)

My desired out is

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 
                        )
                )

        )

)

To remove duplicate elements in a multidimensional aray. The result above is from print_r() in php if this is relevant

1 Answer 1

0

You can use for loop to check before you assigning the student class or deal with the array that contains repetitive classes after output using 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
2
  • its worth a try let me try it Commented Sep 2, 2015 at 11:50
  • Try without [0] as well Commented Sep 2, 2015 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.