2

How do I remove duplicates from a array if it only should check in position [1] (fruit name)

Array ( 
    [0] => Array ( 
        [0] => 1 
        [1] => Apple
    ) 
    [1] => Array ( 
        [0] => 2 
        [1] => Pineapple 
    ) 
    [2] => Array ( 
        [0] => 1 
        [1] => Apple
    ) 
)
5
  • foreach over the parent array, add each value to a new array where the key is [0] from each array. Duplicates will just overwrite each other. You can then use array_values if you wanted to reset the keys back to zero. Commented May 25, 2016 at 16:17
  • What if i have more numbers/informations than the fruit name? Will it still work? Commented May 25, 2016 at 16:19
  • Oh, I was backwards. You can create the array with the key [1] as well. Either one would work. Commented May 25, 2016 at 16:21
  • could you show me? foreach {$array as $item} $array2 = $item[1]; Commented May 25, 2016 at 16:26
  • You showed the starting array. It would be helpful if you showed what the array should look like after duplicates are removed. Commented May 25, 2016 at 16:40

3 Answers 3

2

Use array_unique :

$arr = array_unique($arr, SORT_REGULAR);

the SORT_REGULAR flag compares the elements normally without changing types

http://php.net/manual/fr/function.array-unique.php

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

2 Comments

Its not removing my duplicates.
That's interesting that it does work, but the manual specifically states Note: Note that array_unique() is not intended to work on multi dimensional arrays, which this is.
1

You can just foreach over the array and add each value to a temporary array where the key is [1] from each array. Duplicate values will just overwrite each other. You can then use array_values afterwards if you wanted to reset the keys back to numeric.

Here is an example:

<?php
$arr = array ( 
    array (1, 'Apple'),
    array (2, 'Pineapple'),
    array (1, 'Apple')
);
echo "Before:\n";
print_r($arr);

//create a temp array
$tmp = array();
foreach($arr as $v){
    $tmp[$v[1]] = $v;
}
//reset the keys
$arr = array_values($tmp);

echo "\n\nAfter:\n";
print_r($arr);

Demo: https://3v4l.org/KCECc

Comments

0

The below code may help you.

$array =array ( 
'0' => array ( '0' => 1, '1' => 'Apple'),
'1' => array ( '0' => 2, '1' => 'Pineapple' ), 
'2' => array ( '0' => 1, '1' => 'Apple') ,
);

$new_array = array();
foreach($array as $values){
    $trimmed_array=array_map('trim',$values);
    $new_array[$values[1]] = $values; 
}

print_r(array_values($new_array));

Out Put :

Array
(
  [0] => Array
    (
        [0] => 1
        [1] => Apple
    )

[1] => Array
    (
        [0] => 2
        [1] => Pineapple
    )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.