Jump to content

[SOLVED] unique value from repeating elements


binujayaraj

Recommended Posts

I do have an array with me.

 

$arr = array(

            10, 11, 12, 13, 14,

            10, 11, 12, 13, 14,

            15,

            20, 21, 22, 23,

            20, 21, 22, 23,

            24

            );

 

could any one provide me an optimized code to return unique values from repeating elements.

 

output of the code should be array(15, 24)

 

any help ?

 

Thanks,

Binu Jayaraj

<?php
$arr = array(
             10, 11, 12, 13, 14,
             10, 11, 12, 13, 14,
             15,
             20, 21, 22, 23,
             20, 21, 22, 23,
             24
            );

$count = array_count_values($arr);
foreach ($count as $k=>$v) {
    if ($v === 1) {
        $unique[] = $k;
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.