Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping array values in php

Tags:

arrays

php

I have an array with some value like this:

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

but i want some thing like this:

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

Thanks in advance..

like image 764
PHP Ferrari Avatar asked Feb 28 '23 11:02

PHP Ferrari


2 Answers

If you don't care about the key to value association possibly messing up, you can use this:

$array = array_unique($array);
like image 88
Tatu Ulmanen Avatar answered Mar 07 '23 23:03

Tatu Ulmanen


Although array_unique was mentioned twice now, I feel the answers failed to point out that you have to use the function on the nested array and not the array itself, so here is a usage example

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

which will do what you want.

like image 24
Gordon Avatar answered Mar 08 '23 01:03

Gordon