find index of values from array??

Reply

Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

find index of values from array??

 
0
  #1
Jun 2nd, 2009
hi,
  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??
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: find index of values from array??

 
0
  #2
Jun 2nd, 2009
hi, ppcfreelancer
please post only related to this question..not others..
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,473
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: find index of values from array??

 
0
  #3
Jun 2nd, 2009
Would this code do the trick?
  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. ?>
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 83
Reputation: nish123 is an unknown quantity at this point 
Solved Threads: 0
nish123's Avatar
nish123 nish123 is offline Offline
Junior Poster in Training

Re: find index of values from array??

 
0
  #4
Jun 2nd, 2009
that code works well..
Last edited by nish123; Jun 2nd, 2009 at 9:47 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: find index of values from array??

 
0
  #5
Jun 2nd, 2009
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
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC