Jump to content


Photo

Array Simply question


  • Please log in to reply
13 replies to this topic

#1 Angela

Angela
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 06:10 AM

<?=$wcr[$i['country']]?>
This code prints country name from Array
$wcr=array(
'All countries',
'Albania',
.... etc ...
...'
);
Need to print only value from Array (0, 1, etc.) not country name. How i must change code?
Thank you in advance.
Angela


#2 Zubaz

Zubaz
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 06:34 AM

I'm not totally sure I get what you're asking, but...

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


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 11 May 2006 - 07:04 AM

or do you want


<?=$i['country']?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 Angela

Angela
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 07:07 AM

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

#5 Zubaz

Zubaz
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 07:19 AM

okay then:
<?=array_search($i['country'], $wcr);?>


#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 11 May 2006 - 07:22 AM

foreach ($wcr[$i] as $key=>$value):
    echo $key[$i];
endforeach;
perhaps this?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 Angela

Angela
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 09:03 AM

[!--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:
<?=$wcr[$i['country']]?>
- 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
<A href="http://someurl.com/search.php?from=0&amp;country=0"><?=$wcr[$i['country']]?></A>

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

#8 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 11 May 2006 - 11:55 AM

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;
}

Life's too short for arguing.

#9 Angela

Angela
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 12:33 PM

[!--quoteo(post=373248:date=May 11 2006, 06:55 AM:name=hvle)--][div class=\'quotetop\']QUOTE(hvle @ May 11 2006, 06:55 AM) View Post[/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

#10 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 11 May 2006 - 12:40 PM

I might be jumping into the middle of things here, but you can't do a return in a foreach loop.

Info: PHP Manual


#11 Angela

Angela
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 12:53 PM

It works when:
<? foreach($wcr as $key=>$value)
{ print_r ($i['country']); }?>
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...

#12 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 11 May 2006 - 01:00 PM

Why are you printing an entire array repeatedly that doesn't have ANYTHING to do with the foreach loop?

Info: PHP Manual


#13 Angela

Angela
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 01:08 PM

[!--quoteo(post=373274:date=May 11 2006, 08:00 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 11 2006, 08:00 AM) View Post[/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?

#14 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 12 May 2006 - 06:32 PM

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
}
}






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users