0

I have multidimensional array as follows.

array(
    0 => array(
            'id' => '6',
            'name' => 'Looper',
            'language' => 'hindi'), 
    1 => array(
            'id' => '7',
            'name' => 'Rush',
            'language' => 'hindi'),
    2 => array(
            'id' => '6',
            'name' => 'Looper',
            'language' => 'hindi'));

So I would like to remove an array having same values and it should be like as follows.

array(
    0 => array(
            'id' => '6',
            'name' => 'Looper',
            'language' => 'hindi'), 
    1 => array(
            'id' => '7',
            'name' => 'Rush',
            'language' => 'hindi'));

Please help me to get the solution.

Sanganabasu
  • 943
  • 7
  • 21
  • 39

4 Answers4

2

can use array_unique() for remove duplicate entry from a single multidimensional array in php

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • your answer outputs: Notice: Array to string conversion in C:\web\xampp\htdocs\stupid.php on line 20 Notice: Array to string conversion in C:\web\xampp\htdocs\notgood.php on line 20 Notice: Array to string conversion in C:\web\xampp\htdocs\notgood.php on line 20 ..... – James L. Oct 20 '12 at 04:19
0

ok, maybe you can try with unset(array[2]), this remove an element from an array http://php.net/manual/en/function.unset.php

itaka
  • 399
  • 1
  • 5
  • 16
0

Pass to variable to hold the keys, check using in_array

function assc_array_filter($array, $id)
{
    $keys = array();
    foreach($array as $index => $arr)
    {
        if(in_array($arr[$id], $keys))
        {
            unset($array[$index]);
        }
        else
        {
            $keys[] = $arr[$id];
        }
    }
    return $array;
}

call function as

print_r(assc_array_filter($arr, 'id'));




----UPDATED: switch using array_search

Above code will return the first in array, not the "second set" found on the array, so.. Added a parameter to allow it to update the "first set" that found..

function assc_array_filter($array, $id, $last_updated = false, $strict = false)
{
    $keys = array();
    foreach($array as $index => $arr)
    {
        if(($in_index = array_search($arr[$id], $keys, $strict)) !== false )
        {
            if($last_updated)
            {
                $array[$in_index] = $arr;
            }
            unset($array[$index]);
        }
        else
        {
            $keys[] = $arr[$id];
        }
    }
    return $array;
}

Call as

print_r(assc_array_filter($arr, 'id', true));
Ryan Harne
  • 469
  • 3
  • 7
  • this will work, but in_array should be used for efficiency. It handles multi dimensional arrays so you are not utilizing the most efficient method as prescribed by php.net.. only 3 lines of code without defining the array.. check below for the most efficient answer – James L. Oct 20 '12 at 03:59
  • This should not be a problem at the first place, if the array was formed by using the **"id" as the index**, any duplication should overwrite the previous set. – Ryan Harne Oct 20 '12 at 04:04
  • I'm not saying that you answer won't work , but Sibu is right and has beat us both.. this can be done in one line from his link.. A bit of humility is in order as I've learned something.. you should too. – James L. Oct 20 '12 at 04:06
  • thanks James! but mine was based on the "id" assume it's a key to the filter. And it can be use to check others as well such as "name" & "language". Your method will work if everything the the array set is **exactly** the same. – Ryan Harne Oct 20 '12 at 04:22
  • fair enough.. you are correct if the 'id' is what is in question. Either way, sorry for being grumpy. – James L. Oct 20 '12 at 04:26
0
$oldarr = array(
    0 => array(
            'id' => '6',
            'name' => 'Looper',
            'language' => 'hindi'), 
    1 => array(
            'id' => '7',
            'name' => 'Rush',
            'language' => 'hindi'),
    2 => array(
            'id' => '6',
            'name' => 'Looper',
            'language' => 'hindi'));

$newarr = array();
foreach( $oldarr as $v ) if( !in_array( $v, $newarr ) ) $newarr[] = $v;
print_r( $newarr );

That will take care of your problem.. as of PHP 4.2, in_array() will take an array as the first parameter. array_unique() will not work for this solution.. from php.net: "Note: Note that array_unique() is not intended to work on multi dimensional arrays."

James L.
  • 4,032
  • 1
  • 15
  • 15