2

In php i have an array as an example below called $source which contains

array(5) {
    [0] => array(2) {
               ['id'] => string(18)'619025087225999360'
               ['url'] => 'http//example.com/apple'
           }
    [1] => array(2) {
               ['id'] => string(18)'623368621227847680'
               ['url'] => 'http//example.com/orange'
           }
    [2] => array(2) {
               ['id'] => string(18)'623368621227847680'
               ['url'] => 'http//example.com/blackberry'
           }
}

So in the above array you can see index number '1' & '2' have the same 'id'. This could be a huge array with repeated 'id' appearing a few times.

What i wanna do is if there is same id i just wanna pick up the first 'id' and discard the other one. I tried to solve this using array_unique() but could not apply it properly. Can someone please help me out here ??

5 Answers 5

3

Hello you can use the below solution for removing duplicates.

$foo = [];
foreach ($arr as $key => $value) {
    if (is_array($foo['id']) && !in_array($value['id'], $foo['id'])) {
        $foo['id'][]  = $value['id'];
        $foo['url'][] = $value['url'];
    }

}

The final $foo will give you the required array

Sign up to request clarification or add additional context in comments.

Comments

1

make new array and copy unique elements from old
$source_new = array(); foreach($source as $v) if (!isset($source_new[$v['id']])) $source_new[$v['id']] = $v; $source = array_values($source_new);

Comments

1

If you have control over the code that generates this array, you might want to use the id as array key:

// input values in loop:
$id = '619025087225999360';
$url = 'http//example.com/apple'

if (!array_key_exists($id, $array)) {
    $array[$id] = array(
            'id' => $id,
            'url' => $url,
        );
}

This also prevents looping through all items repeatedly. Especially if the array is big, it will be faster and more memory efficient.

If this is not possible, I'd suggest this code to remove duplicates efficiently:

$usedIds = array();
foreach ($array as $key => $value) {
    if (array_key_exists($value['id'], $usedIds)) {
        unset($array[$key]);
    }
    $usedIds[$value['id']] = true;
}

Advantages:

  • does not create a copy of the array
  • only loops once over all items
  • uses a hashmap to check for existing ids (fast access)

Comments

0

Cleaner , Simpler :

de-duplicate results from a variety of overlapping queries.

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

3 Comments

And doesn't work for this scenario because there ar no exact duplicates but only the id is duplicated.
I thought you need to remove all the duplicate sub array.
Look at the example: there's orange and blackberry
0

Hey I have created a code for you..Its very simple with no extra methods and it gets the job done.

<?php

$source = array(
    array(
        'id' => '619025087225999360',
        'url' => 'http//example.com/apple'
    ),
    array(
        'id' => '623368621227847680',
        'url' => 'http//example.com/orange'
    ),
    array(
        'id' => '623368621227847680',
        'url' => 'http//example.com/blackberry'
    )
);

$newArray = array();
foreach($source as $data){

    $flag = TRUE;
    foreach($newArray as $newData){
        if($newData['id']==$data['id']){
            $flag = FALSE;
        }
    }
    if($flag){
        $newArray []= $data;
    }
}
echo '<pre>';
echo '<h1>Previous Array</h1><hr>';
print_r($source);
echo '<h1>New Array</h1><hr>';
print_r($newArray);
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.