0

First i will like to say that, i have looked into other post but failed trying to accomplish my needs. I have used array_unique($array) but the duplicates don't get discarded. This is a view of my array using var_dump:

{
  [0]=>
  string(12) "44.94.192.40"
}
array(1) {
  [0]=>
  string(12) "44.94.192.41"
}
array(1) {
  [0]=>
  string(9) "44.94.1.1"
}
array(1) {
  [0]=>
  string(9) "44.94.1.1"
}
array(1) {
  [0]=>
  string(13) "44.96.253.100"
}

"44.94.1.1" is a duplicate which i hope to remove but i can't. Does this have to do with my array structure ?

4 Answers4

0

Edited in response to your comment:

From the documentation on array_unique():

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

So the fact that each of your values is an array is interfering.

Try creating an array containing just the string values:

$new_array = array();
foreach ($array as $value) {
    $new_array[] = $value[0];
}
$new_array = array_unique($new_array);
Gabriel Roth
  • 1,030
  • 1
  • 12
  • 31
  • Sorry i meant array_unique(); that what i have tried but it doesn't work –  Feb 20 '14 at 15:22
0

You can do it with a foreach loop

$array = array(
  array("44.94.192.40"),
  array("44.94.192.41"),
  array("44.94.1.1"),
  array("44.94.1.1"),
  array("44.96.253.100"),
);
$temp_arr=array();
foreach($array as $elementKey => $element)
{
  foreach($element as $valueKey => $value)
  {
    if(in_array($value,$temp_arr)) unset($array[$elementKey]);
    else $temp_arr[]=$value;
  }
}
print_r($array);

Output would be:

Array ( [0] => Array ( [0] => 44.94.192.40 ) [1] => Array ( [0] => 44.94.192.41 ) [2] => Array ( [0] => 44.94.1.1 ) [4] => Array ( [0] => 44.96.253.100 ) )
131
  • 1,363
  • 1
  • 12
  • 32
0

You can flatten your array and do array unique or do array_map with a reference to a foreign array

<?php
$data = array(
  array("44.94.192.40"),
  array("44.94.192.41"),
  array("44.94.1.1"),
  array("44.94.1.1"),
  array("44.96.253.100"),
);

$visited = array();

array_map(function($v) use ( &$visited ){
   if(array_search( $v[0], $visited ) === false){
     $visited[] = $v[0];
   };
},$data);

var_dump($visited);

array(4) {
  [0]=>
  string(12) "44.94.192.40"
  [1]=>
  string(12) "44.94.192.41"
  [2]=>
  string(9) "44.94.1.1"
  [3]=>
  string(13) "44.96.253.100"
}
markcial
  • 9,041
  • 4
  • 31
  • 41
0

You can find a variety of ways to remove duplicates from a multidimensional array on the php doc page of array_unique in the user comments

http://us3.php.net/array_unique

Orel also mentioned a duplicate question that at a glance also contains a function for doing the same How to remove duplicate values from an array in PHP

For simplicity I have picked one for you, but depending on exactly what you want, there are many different flavors on the php page I linked

foreach ($arrAddressList AS $key => $arrAddress) {
    $arrAddressList[$key] = serialize($arrAddress);
}
$arrAddressList = array_unique($arrAdressList);
foreach ($arrAddressList AS $key => $strAddress) {
    $arrAddressList[$key] = unserialize($strAddress);
}
Community
  • 1
  • 1
Tim
  • 491
  • 3
  • 6
  • Thanks to Rocket Hazmat for kindly editing the code snippet, I was struggling to format it from my phone browser – Tim Feb 20 '14 at 15:55