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.

2
  • 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. Commented Aug 17, 2018 at 7: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. Commented Aug 17, 2018 at 7:51

2 Answers 2

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

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

2 Comments

Nice use of the third parameter of array_column. I hadn't seen it used that way.
Me neither, but I just figured it should work. And it did.
0

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

$arrayOrdenado = array(); foreach($array as $a){ $arrayOrdenado[$a["value"]][] = $a; } 

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.