hi,

$array = array("amit", "hello", "amit", "world", "hello");
print_r(array_count_values($array));

out put
Array ( [amit] => 2 [hello] => 2 [world] => 1 )

i want to find the index of amit i.e
amit -> 0 and 2

hello -> 1 and 4

world -> 3

How to do this??

Recommended Answers

All 4 Replies

hi, ppcfreelancer
please post only related to this question..not others..

Would this code do the trick?

<?
$array = array("amit", "hello", "amit", "world", "hello");

/* Sort into an array */
$i=0;
foreach ($array AS $value){
    $result[$value][]=$i;
    $i++;
    }
/* Dump Result */
echo '<xmp>';
print_r($result);
echo '</xmp><hr>';

/* Display Results */
foreach ($result AS $name=>$var) {
    echo $name.' ';
    foreach ($var AS $arrayposition) {
        echo $arrayposition.', ';
        }
    echo '<br>';
    }
?>

that code works well..

Don't do that, there's a built-in PHP function. array_keys($array, "search_key") , that returns an array containing the indices for the searc_key

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.