Remove duplicate ID inside array

Remove duplicate ID inside array



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
)
}






array_unique() returns a new array. What happens if you try $array = array_unique($array);?

– rickdenhaan
Sep 15 '18 at 15:17


array_unique()


$array = array_unique($array);






What do you want to happen to the title data? As you will have a single record_num but multiple, different, title element values

– Martin
Sep 15 '18 at 15:18



title


record_num


title






Yes, it should work only for simple arrays. Please see another functions or make an additional loops.

– toor
Sep 15 '18 at 15:19






check this out from the php array_unique documentation itself php.net/manual/en/function.array-unique.php#116302

– Alleo Indong
Sep 15 '18 at 15:21






@rickdenhaan same.

– Darko1996
Sep 15 '18 at 15:25




3 Answers
3



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');






Hmm looks promising, i will try it now

– Darko1996
Sep 15 '18 at 15:49






Working, it's a just what i need

– Darko1996
Sep 15 '18 at 16:07



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.


null


record_num


$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






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 php.net/manual/en/function.array-column.php#117229

– Always Sunny
Sep 15 '18 at 15:43



array_column()






@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



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


array_map()


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



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)