943,762 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 6640
  • PHP RSS
Jun 2nd, 2009
0

find index of values from array??

Expand Post »
hi,
PHP Syntax (Toggle Plain Text)
  1. $array = array("amit", "hello", "amit", "world", "hello");
  2. 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??
Similar Threads
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
Jun 2nd, 2009
0

Re: find index of values from array??

hi, ppcfreelancer
please post only related to this question..not others..
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
Jun 2nd, 2009
0

Re: find index of values from array??

Would this code do the trick?
php Syntax (Toggle Plain Text)
  1. <?
  2. $array = array("amit", "hello", "amit", "world", "hello");
  3.  
  4. /* Sort into an array */
  5. $i=0;
  6. foreach ($array AS $value){
  7. $result[$value][]=$i;
  8. $i++;
  9. }
  10. /* Dump Result */
  11. echo '<xmp>';
  12. print_r($result);
  13. echo '</xmp><hr>';
  14.  
  15. /* Display Results */
  16. foreach ($result AS $name=>$var) {
  17. echo $name.' ';
  18. foreach ($var AS $arrayposition) {
  19. echo $arrayposition.', ';
  20. }
  21. echo '<br>';
  22. }
  23. ?>
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Jun 2nd, 2009
0

Re: find index of values from array??

that code works well..
Last edited by nish123; Jun 2nd, 2009 at 9:47 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
nish123 is offline Offline
83 posts
since Apr 2009
Jun 2nd, 2009
0

Re: find index of values from array??

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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: video tutorial
Next Thread in PHP Forum Timeline: Update Status MySpace and Linkedin via PHP API





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC