Angela Posted May 11, 2006 Share Posted May 11, 2006 [code]<?=$wcr[$i['country']]?>[/code]This code prints country name from Array [code]$wcr=array('All countries','Albania',.... etc ......');[/code]Need to print only value from Array (0, 1, etc.) not country name. How i must change code?Thank you in advance.Angela Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/ Share on other sites More sharing options...
Zubaz Posted May 11, 2006 Share Posted May 11, 2006 I'm not totally sure I get what you're asking, but...[code]<?echo array_search($i['country'], $wcr); ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35170 Share on other sites More sharing options...
Barand Posted May 11, 2006 Share Posted May 11, 2006 or do you want[code]<?=$i['country']?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35176 Share on other sites More sharing options...
Angela Posted May 11, 2006 Author Share Posted May 11, 2006 Not works. Thanks anyway.I need not echo, just how to retreive as Array keys - 0, 1, 2 etc (not their values -Albania, etc). Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35178 Share on other sites More sharing options...
Zubaz Posted May 11, 2006 Share Posted May 11, 2006 okay then:[code]<?=array_search($i['country'], $wcr);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35183 Share on other sites More sharing options...
.josh Posted May 11, 2006 Share Posted May 11, 2006 [code]foreach ($wcr[$i] as $key=>$value): echo $key[$i];endforeach;[/code]perhaps this? Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35185 Share on other sites More sharing options...
Angela Posted May 11, 2006 Author Share Posted May 11, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]foreach ($wcr[$i] as $key=>$value): echo $key[$i];endforeach;[/quote]not works again : Invalid argument supplied for foreach() [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<?=$i['country']?> [/quote] - get no resultsWhat i need: [code]<?=$wcr[$i['country']]?> [/code]- it gets value from array (i.e. Germany)Need to get ONLY Array key. i.e number for Germany in array $wcr. Then need no echo, i want to insert this varable into link [code]<A href="http://someurl.com/search.php?from=0&country=0"><?=$wcr[$i['country']]?></A>[/code]Now country=0. need to change it, when country will change. Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35210 Share on other sites More sharing options...
hvle Posted May 11, 2006 Share Posted May 11, 2006 I suppose you have the country name, and you need to find out key location.Here is one way to do it:$country = "Germany"; // say you want to find key location for Germanyforeach ($wcr as $key=>$val){ if ($val == $country) return $key;} Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35227 Share on other sites More sharing options...
Angela Posted May 11, 2006 Author Share Posted May 11, 2006 [!--quoteo(post=373248:date=May 11 2006, 06:55 AM:name=hvle)--][div class=\'quotetop\']QUOTE(hvle @ May 11 2006, 06:55 AM) [snapback]373248[/snapback][/div][div class=\'quotemain\'][!--quotec--]I suppose you have the country name, and you need to find out key location.Here is one way to do it:$country = "Germany"; // say you want to find key location for Germanyforeach ($wcr as $key=>$val){ if ($val == $country) return $key;}[/quote]Yes, you are right it so easy, but just courious - NOT WORKS :-( no results Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35237 Share on other sites More sharing options...
ober Posted May 11, 2006 Share Posted May 11, 2006 I might be jumping into the middle of things here, but you can't do a return in a foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35242 Share on other sites More sharing options...
Angela Posted May 11, 2006 Author Share Posted May 11, 2006 It works when:[code]<? foreach($wcr as $key=>$value){ print_r ($i['country']); }?>[/code]but need some correcting in code, because prints as key too many copies like this: 77777777777777777777....7How to change that get without copies?Maybe someone can help... Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35247 Share on other sites More sharing options...
ober Posted May 11, 2006 Share Posted May 11, 2006 Why are you printing an entire array repeatedly that doesn't have ANYTHING to do with the foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35250 Share on other sites More sharing options...
Angela Posted May 11, 2006 Author Share Posted May 11, 2006 [!--quoteo(post=373274:date=May 11 2006, 08:00 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 11 2006, 08:00 AM) [snapback]373274[/snapback][/div][div class=\'quotemain\'][!--quotec--]Why are you printing an entire array repeatedly that doesn't have ANYTHING to do with the foreach loop?[/quote]I don't know. ober please tell me what can i do? Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35252 Share on other sites More sharing options...
emehrkay Posted May 12, 2006 Share Posted May 12, 2006 so you want to print each key in the array? or do you want to print the key that matches a specific country?to print each key, someone wrote this earlier with the wrong syntaxforeach($wcr as $key => $value){echo $key."<br />";}to mactch simply provide the country$country = "Albania";foreach($wcr as $key => $value){if($value == $country){echo $key."<br />";break; //im not too sure if you need it or if it works in this type of loop}} Quote Link to comment https://forums.phpfreaks.com/topic/9526-array-simply-question/#findComment-35257 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.