0

I am in a situation to make an array unique by the subarray index.Please refer this code and help me to solve this.

    $user=Array(
       [1]=>Array(
                 username => Sujith,
                 email=>someone@example.com,
                 [address] => Array(
                    [address_id] => 1227
                    [city] => Ayoor
                    [state] => Kerala
                    [Country] => India
                 )
      )
     [2]=>Array(
         username => Ajith,
         email=>someone2@example.com,
         [address] => Array(
            [address_id] => 1227
            [city] => Ayoor
            [state] => Kerala
            [Country] => India
        )
     )
     [3]=>Array(
         username => Akhil,
         email=>someone3@example.com,
         [address] => Array(
            [address_id] => 1228
            [city] => Kollam
            [state] => Kerala
            [Country] => India
        )

    )
 )

I want to make it unique by the address.address_id.So the resulting array should be

    $user=Array(
           [1]=>Array(
                     username => Sujith,
                     email=>someone@example.com,
                     [address] => Array(
                        [address_id] => 1227
                        [city] => Ayoor
                        [state] => Kerala
                        [Country] => India
                     )
          )
         [3]=>Array(
             username => Akhil,
             email=>someone3@example.com,
             [address] => Array(
                [address_id] => 1228
                [city] => Kollam
                [state] => Kerala
                [Country] => India
            )

        )
     )

I mean ,1 user per address..Please help me to solve this..

sujithayur
  • 376
  • 3
  • 13

5 Answers5

1

Loop through the $user array and unset() users, who's address_id has already occurred.

You can use an auxiliary array to keep track of address_ids that have occurred.

$addresses = array();
foreach ($user as $key => $value) {
    $id = $value['address']['address_id'];
    if (in_array($id, $addresses)) {
        unset($user[$key]);
    } else {
        $addresses[] = $id;
    }
}
unset($addresses);
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • Yes this is how it should work since there is no inbuilt function to do the operation. Using array_map and array_unique together we can get the unique address_id but this will require additional loop to generate the array as desired. Also with array_filter you need additional loop for generating the array. So yes in simple way your answer is pretty good !! – Abhik Chakraborty Mar 14 '14 at 09:11
1

A short solution:

$ids = [];
$result = array_filter($user, function ($item) use (&$ids) {
    $id = $item['address']['address_id']; //or whatever you want to be unique
    if (in_array($id, $ids)) return false;

    $ids[] = $id;
    return true;
});
pawle
  • 21
  • 3
0
$tmpArray = array();
$newArr = array();
foreach($users as $user)
{

if(!in_array($user['address']['address_id'],$tmpArray))
  $newArr[] = $user;
$tmpArray[] = $user['address']['address_id']
}
var_dump($newArr);
MIIB
  • 1,849
  • 10
  • 21
0
   $unique_array = array();
   $address_id_accumulator = array();
    foreach ($user as $k => $v) {         
        if ( !in_array( $v['address']['address_id'], $address_id_accumulator) ){
            $unique_array[$k] = $v;
        }
        $address_id_accumulator[] = $v['address']['address_id'];         
    }

Your $user array is the input here. $unique_array will be $user array with unique address id's.

-2

There is no native PHP function doing explicitly what you want, but you could: - Do you own function, this takes 2 minutes - Use array_filter()

Loenix
  • 1,057
  • 1
  • 10
  • 23