foreach in foreach multiple dimensional

Morgan Tartreau

I'd like to know how to avoid dupplicate of element while using a foreach in foreach with multidimensional array ?

The first level of my array can have several item (here's just 2, but maybe I can have 7 level). I've a lot trouble with this. Then this ID is going to be used as a parameter in a sql request, but this is another story.

This is my array :

Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                        )
                    [1] => Array
                        (
                            [id] => 11
                        )
                    [2] => Array
                        (
                            [id] => 12
                        )
                )
            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 11
                        )
                    [1] => Array
                        (
                            [id] => 12
                        )
                )
        )

This is my foreach loop :

foreach($dataListe as $listeDiff){
            foreach($listeDiff as $$item){
                // echo $item[0].'<br />';
                echo "<pre>".print_r($item, true)."</pre>";
            }
        }

Result :

        Array
(
    [id] => 10
)

Array
(
    [id] => 11
)

Array
(
    [id] => 12
)

Array
(
    [id] => 11
)

Array
(
    [id] => 12
)

Wanted :

        Array
(
    [id] => 10
)

Array
(
    [id] => 11
)

Array
(
    [id] => 12
)
J. Doe

use array_unique()

$result = [];
foreach($dataListe as $listeDiff){
   $result[] = $listeDiff;        
}
$result = array_unique($result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive