954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

find index of values from array??

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

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

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

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

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 '';
    }
?>
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

that code works well..

nish123
Junior Poster in Training
83 posts since Apr 2009
Reputation Points: 10
Solved Threads: 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

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You