Jump to content

Remove duplicates froma multidimensional array?


easyedy

Recommended Posts

foreach (array($item[$FA][$SA]) as $item) {
echo $item['Name']; }

 

It just returns the first 'Name' of the array....

 

 

Here is what the array looks like.:

 

Array

      (

          [$FA] => Array

              (

                  [$SA] => Array

                      (

                          [0] => Array

                              (

                                  [iD] => 390517

                                  [Name] => Bill

 

                          [1] => Array

                              (

                                  [iD] => 390519

                                  [Name] => Mike

 

                          [3] => Array

                              (

                                  [iD] => 390533

                                  [Name] => Sam

 

                          )

 

 

                    )

 

              )

 

 

try

<?php
$items = array
      (
          'x' => array
              (
                  'y' => array
                      (
                          0 => array
                              (
                                  'ID' => 390517,
                                  'Name' => 'Bill'
                                  ),
                          1 => array
                              (
                                  'ID' => 390519,
                                  'Name' => 'Mike'
                                  ),
                          2 => array
                              (
                                  'ID' => 390533,
                                  'Name' => 'Sam'
                                  ),
                          3 => array
                              (
                                  'ID' => 390519,
                                  'Name' => 'Mike'
                                  )
                     )
              )
      );

/**
* remove dupes
*/
$a = $items['x']['y'];
$k = count($a);
for($i=0; $i < $k-1; $i++)
{
    for($j=$i+1; $j < $k; $j++)
    {
        if ($a[$i] == $a[$j]) unset($a[$j]);
    }
}
$items['x']['y'] = $a;
echo '<pre>', print_r($items, true), '</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.