1

Consider the same array but id of 3rd index is different:

$all_array = Array
(
    [0] => Array
        (
            [id] => 1
            [value] => 111
        )

    [1] => Array
        (
            [id] => 2
            [value] => 222
        )

    [2] => Array
        (
            [id] => 3
            [value] => 333
        )

    [3] => Array
        (
            [id] => 4
            [value] => 111
        )
)

Now, both 1 & 4 have same values. So we want to remove any of them:

$unique_arr = array_unique( array_column( $all_array , 'value' ) );
print_r( array_intersect_key( $all_array, $unique_arr ) );
4
  • Remove all array or just value ? Commented Sep 20, 2022 at 13:45
  • 1
    Do you want to remove both 1 & 4 or Just 1 or Just 4 Commented Sep 20, 2022 at 13:49
  • When posting example data of arrays, please use var_export($theArray) and post the result of that. It will output the array in valid PHP syntax that we can use when testing/answering. Commented Sep 20, 2022 at 13:57
  • Looks a bit like this answer: How to remove duplicate values from a multi-dimensional array in PHP Commented Nov 26, 2024 at 3:35

2 Answers 2

1

Without foreach with array_column.

$array = array_column($all_array,null,'value');
$array = array_values($array);  //if the array is to be re-indexed

var_dump($array);

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

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

Comments

0

You can use foreach() for it:

$finalArray = [];

foreach($all_array as $arr){
    
    $finalArray[$arr['value']] = $arr;
}

print_r($finalArray);
print_r(array_values($finalArray)); // to re-index array

https://3v4l.org/82XUI

Note: this code will give you always the last index data of the duplicate once

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.