Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4896

how to get the non repetitive value from an array in PHP

I have an array $array when i do print_r() the out put is something like this

Array ( [0] => Array ( [0] => Puebla [1] => San Pedro Cholula [2] => Colonia Ampliación Momoxpan, San Pedro Cholula ) 
        [1] => Array ( [0] => Veracruz [1] => Papantla [2] => Colonia Papantla Centro, Papantla de Olarte ) 
        [2] => Array ( [0] => Veracruz [1] => Veracruz [2] => Colonia Veracruz Centro, Veracruz ) 
        [3] => Array ( [0] => Veracruz [1] => Veracruz [2] => Colonia Veracruz Centro, Veracruz ) 
        [4] => Array ( [0] => Morelos [1] => Emiliano Zapata [2] => Unidad habitacional Tezoyuca, Emiliano Zapata ) 
        [5] => Array ( [0] => Puebla [1] => San Pedro Cholula [2] => Residencial Momoxpan, San Pedro Cholula ) 
        [6] => Array ( [0] => Puebla [1] => Puebla de Zaragoza [2] => Colonia Villa de Reyes, Puebla ) 
        [7] => Array ( [0] => Puebla [1] => Puebla de Zaragoza [2] => Colonia San Isidro Castillotla, Puebla ) 
        [8] => Array ( [0] => Puebla [1] => Puebla de Zaragoza [2] => Colonia Popular Castillotla, Puebla ) 
        [9] => Array ( [0] => Veracruz [1] => Alvarado [2] => Fraccionamiento Lomas Residencial, Alvarado ) 
        [10] => Array ( [0] => Veracruz [1] => Alvarado [2] => Fraccionamiento Lomas Diamante, Alvarado ) 
        [11] => Array ( [0] => Puebla [1] => San Andrés Cholula [2] => Fraccionamiento Valle Real, San Andrés Cholula ) 
        [12] => Array ( [0] => Puebla [1] => San Andrés Cholula [2] => Fraccionamiento Rincón de Atlixcayotl, San Andrés Cholula ) );

I am trying to get the non repetitive arrays out of it but unfortunately when I use array_unique() function it returns only one array out of this

I was trying to use like this

$array = array_unique($array);

and the out put of this was

Array ( [0] => Array ( [0] => Puebla [1] => San Pedro Cholula [2] => Colonia Ampliación Momoxpan, San Pedro Cholula ) ) 

Which is wrong because i have other array's too that are unique

Please help me out

Thanks & Regards

Upvotes: 0

Views: 164

Answers (2)

rasso
rasso

Reputation: 2221

From a comment in manual

array_unique is not intended to work with multi-dimensional arrays, it does on 5.2.9. However, it does not for 5.2.5.

So you have to serialize the array first before you pass it to array_unique function and then unserialize using array_map as suggested in How to remove duplicate values from a multi-dimensional array in PHP

EDIT

In your case it should be as follows

$arr = array_map("unserialize", array_unique(array_map("serialize", $array)));
print_r($arr);

Upvotes: 2

Rahul
Rahul

Reputation: 211

array_unique don't work for multidimensional array. Now you can get your 
exact answer form here:

 http://schoolsofweb.com/how-to-remove-duplicate-values-from-a-multidimensional-array-in-php/

 OR

 http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php

 OR

 http://phpdevblog.niknovo.com/2009/01/using-array-unique-with-multidimensional-arrays.html

 Please read it carefully first and then apply it on your code. If still not solved then let me comment.

Upvotes: 0

Related Questions