Jump to content

Array Simply question


Angela

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
Link to comment
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.
Link to comment
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;
}
Link to comment
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
Link to comment
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...
Link to comment
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?
Link to comment
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
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.