Question

Ok I am pretty sure there is a simple solution, and I am missing something on this but

lets say I have this a simple array:

Array
(
    [0] => 79990
    [1] => 79040
    [2] => 79100
    [3] => 79990
    [4] => 79490
    [5] => 79290
    [6] => 79990
)

0, 3 and 6 are the same value

how do I mark/highlight these values on a foreach loop? result should be something like:

Array
(
    [0] => *79990*
    [1] => 79040
    [2] => 79100
    [3] => *79990*
    [4] => 79490
    [5] => 79290
    [6] => *79990*
)

edit: typos

Was it helpful?

Solution

This should do the trick:

<?php

    $array = array( '79900',
                    '79040',
                    '79100',
                    '79990',
                    '79490',
                    '79290',
                    '79990');

    $count = array_count_values($array);

    echo "<pre>".print_r($array, true)."</pre>";

    foreach($array as $val)
    {
        if($count[$val]>1) {
            $output[] = "*".$val."*";
        } else {
            $output[] = $val;
        }
    }

    echo "<pre>".print_r($output, true)."</pre>";

?>

Outputs:

Array
(
    [0] => 79900
    [1] => 79040
    [2] => 79100
    [3] => 79990
    [4] => 79490
    [5] => 79290
    [6] => 79990
)

Array
(
    [0] => 79900
    [1] => 79040
    [2] => 79100
    [3] => *79990*
    [4] => 79490
    [5] => 79290
    [6] => *79990*
)

Note: Your [0] isn't actually the same as [3] and [6], but I'm assuming this is just a typo

Let me know how you get on!

OTHER TIPS

$array = array("79900","79040","79100","79990","79490","79290","79990");
$count = array_count_values( $array );
$list  = array();
foreach( $count as $index => $value ){
    if( $value > 1 ){
        $list[] = "*" . $index . "*";
    }else{
        $list[] = $index;
    }
}

Note that the repeated index is removed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top