0

Referring to this question & this, i have a little different problem here.

I have an array like this:

Array
(
    [0] => Array
    (
        [name] => "Size"
        [value] => "Large"
        [id] => "1201"
    )

    [1] => Array
    (
        [name] => "Size"
        [value] => "Small"
        [id] => "1203"
    )

    [2] => Array
    (
        [name] => "Size"
        [value] => "Medium"
        [id] => "1204"
    )

    [3] => Array
    (
        [name] => "Size"
        [value] => "Large"
        [id] => "1205"
    )

    [4] => Array
    (
        [name] => "Size"
        [value] => "Large"
        [id] => "1206"
    )

    [5] => Array
    (
        [name] => "Size"
        [value] => "Large"
        [id] => "1207"
    )

)

Above array have repetition of Large Three times, i want to identify unique on key based value. and remove that index (0,1,2,3,4,5) from that array.

Mentioned questions contains problems like this, but not the exact problem i am facing.

I am trying like this:

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

but not working.

Noman Ali
  • 3,160
  • 10
  • 43
  • 77
  • So what "Id" is going to stay? They all have different id. It would be very helpful if you posted the array as a var_export or json_encode. – Andreas Aug 17 '18 at 07:51
  • You may have repetition of Large but the ID is different. If you got this data from the database it's probably better to remove duplicates at that point instead of at the PHP end. – apokryfos Aug 17 '18 at 07:51

2 Answers2

1

Since you have not answered my question yet I assume "id" is irrelevant.

By using array_column to make the array associative on "value" and it will delete any duplicates, then array_values will reset the keys to indexed.

This way you don't have to loop at all.

$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);

output:

array(3) {
  [0]=>
  array(3) {
    ["name"]=>
    string(4) "Size"
    ["value"]=>
    string(5) "Large"
    ["id"]=>
    string(4) "1207"
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(4) "Size"
    ["value"]=>
    string(5) "Small"
    ["id"]=>
    string(4) "1203"
  }
  [2]=>
  array(3) {
    ["name"]=>
    string(4) "Size"
    ["value"]=>
    string(6) "Medium"
    ["id"]=>
    string(4) "1204"
  }
}

https://3v4l.org/aOhVS


If you want to keep the lowest "id", and the "id" is higher the further down in the array you go (as in your example), then you can use rsort($arr); before the code.

rsort($arr);
$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);

output:

array(3) {
  [0]=>
  array(3) {
    ["name"]=>
    string(4) "Size"
    ["value"]=>
    string(5) "Small"
    ["id"]=>
    string(4) "1203"
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(4) "Size"
    ["value"]=>
    string(6) "Medium"
    ["id"]=>
    string(4) "1204"
  }
  [2]=>
  array(3) {
    ["name"]=>
    string(4) "Size"
    ["value"]=>
    string(5) "Large"
    ["id"]=>
    string(4) "1201"
  }
}

https://3v4l.org/VtgAM

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

You could try make a foreach creating another array like you need

$arrayOrdenado = array();
foreach($array as $a){
 $arrayOrdenado[$a["value"]][] = $a;
}
Andreas
  • 23,610
  • 6
  • 30
  • 62