1

I have try to remove duplicated post with array_unique but seems not work in this way, i have try to add array_unique($array) after while but it's back me only one result, record_num is ID.

while($row = mysql_fetch_assoc($result)) {
        $array[] = $row;
}
array_unique($array)
foreach($array as $row) {
   .....
}

Array example

Array
(
    [0] => Array
        (
            [record_num] => 18152
            [title] => Title of post 
        )

    [1] => Array
        (
            [record_num] => 18150
            [title] => Title of post 2
        )

    [2] => Array
        (
            [record_num] => 18134
            [title] => Title of post 3
        )
    [3] => Array
        (
            [record_num] => 18134
            [title] => Title of post 3
        )
}
Darko1996
  • 27
  • 6

3 Answers3

7

You can use array_column to remove duplicates from a multidimensional array.
Array_column returns one column of an array, but if you set the second parameter to null and the third to record_num it will remove the duplicates.

$arr = array_values(array_column($arr, null, 'record_num'));

Output:

array(3) {
  [0]=>
  array(2) {
    ["record_num"]=>
    int(18152)
    ["title"]=>
    string(14) "Title of post "
  }
  [1]=>
  array(2) {
    ["record_num"]=>
    int(18150)
    ["title"]=>
    string(15) "Title of post 2"
  }
  [2]=>
  array(2) {
    ["record_num"]=>
    int(18134)
    ["title"]=>
    string(15) "Title of post 3"
  }
}

https://3v4l.org/jEvWG

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Yeah but array_column is a function in PHP 5.5+ i have older – Darko1996 Sep 15 '18 at 15:39
  • @Darko1996 if you have problem with `array_column()` because of php version then you can use the User Contributed code at php.net for lower version of php. See http://php.net/manual/en/function.array-column.php#117229 – A l w a y s S u n n y Sep 15 '18 at 15:43
  • @Darko1996 why are you using that old version of PHP? There are plenty of benefits if you upgrade – Andreas Sep 15 '18 at 15:46
  • It's a old project need to update 1000+ files for new version of php :( – Darko1996 Sep 15 '18 at 15:48
  • You don't always have to update the PHP files just because you upgrade the version of the compiler. Some or most functions work but may display an notice. – Andreas Sep 15 '18 at 15:55
  • yeah i know, definitely i will do when i have more free time – Darko1996 Sep 15 '18 at 16:08
2

Use a function like this

function unique_multidim_array($array, $key) { 
$temp_array = array(); 
$i = 0; 
$key_array = array(); 

foreach($array as $val) { 
    if (!in_array($val[$key], $key_array)) { 
        $key_array[$i] = $val[$key]; 
        $temp_array[$i] = $val; 
    } 
    $i++; 
} 
return $temp_array; 
}

and call it like

unique_multidim_array($details,'record_num');
Rinsad Ahmed
  • 1,877
  • 1
  • 10
  • 28
1

Actually you can do it by multiple way, This way also work for you with array_map() and array_unique()

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

RESULT:

Array ( 
 [0] => Array ( 
   [record_num] => 18152
   [title] => Title of post 
 )
 [1] => Array ( 
   [record_num] => 18150
   [title] => Title of post 2 
 ) 
 [2] => Array (
   [record_num] => 18134
   [title] => Title of post 3 
 )
)

DEMO: https://3v4l.org/PbRAp

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103