Jump to content

Archived

This topic is now archived and is closed to further replies.

Angela

Array Simply question

Recommended Posts

[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

Share this post


Link to post
Share on other sites
I'm not totally sure I get what you're asking, but...

[code]
<?
echo array_search($i['country'], $wcr);
?>
[/code]

Share this post


Link to post
Share on other sites
Not works. Thanks anyway.
I need not echo, just how to retreive as Array keys - 0, 1, 2 etc (not their values -Albania, etc).

Share this post


Link to post
Share on other sites
okay then:
[code]
<?=array_search($i['country'], $wcr);?>
[/code]

Share this post


Link to post
Share on other sites
[code]
foreach ($wcr[$i] as $key=>$value):
    echo $key[$i];
endforeach;
[/code]
perhaps this?

Share this post


Link to post
Share on other sites
[!--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 results

What 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&amp;country=0"><?=$wcr[$i['country']]?></A>[/code]

Now country=0. need to change it, when country will change.

Share this post


Link to post
Share on other sites
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 Germany

foreach ($wcr as $key=>$val)
{
if ($val == $country)
return $key;
}

Share this post


Link to post
Share on other sites
[!--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 Germany

foreach ($wcr as $key=>$val)
{
if ($val == $country)
return $key;
}
[/quote]

Yes, you are right it so easy, but just courious - NOT WORKS :-( no results

Share this post


Link to post
Share on other sites
I might be jumping into the middle of things here, but you can't do a return in a foreach loop.

Share this post


Link to post
Share on other sites
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....7
How to change that get without copies?
Maybe someone can help...

Share this post


Link to post
Share on other sites
Why are you printing an entire array repeatedly that doesn't have ANYTHING to do with the foreach loop?

Share this post


Link to post
Share on other sites
[!--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?

Share this post


Link to post
Share on other sites
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 syntax

foreach($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
}
}

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.