Grouping array values ​​in php

I have an array with some value like this:

[Organization_id] => Array ( [0] => 4 [1] => 4 [2] => 4 ) 

but I want something like this:

 [Organization_id] => Array ( [0] => 4 ) 

Thanks in advance.

+4
source share
2 answers

If you are not interested in using a key to combine values, you can use this:

 $array = array_unique($array); 
+4
source

Although array_unique has already been mentioned twice, I feel that the answers do not indicate that you should use the function on a nested array and not the array itself, so here is an example of use

 $array = array( 'Organization_id' => array(4,4,4) ); $array['Organization_id'] = array_unique( $array['Organization_id'] ); print_r($array); 

who will do what you want.

+4
source

Source: https://habr.com/ru/post/1303094/


All Articles