0

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??

4
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by ShawnCplus
0

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

0

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>';
    }
?>
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.