need a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

need a function

Post by m2babaey »

Hi
I have the list of all countries in this format:
""=>Armenia,
""=>Aruba,
""=>Australia,
""=>Austria,
""=>Azerbaijan,
""=>Bahamas,
""=>Bahrain,
""=>Bangladesh,
""=>Barbados,
I want to make it in the following format: (copy name between ""):
"Afghanistan"=>Afghanistan,
"Albania"=>Albania,
"Algeria"=>Albania,
"Andorra"=>Andorra,
"Angola"=>Angola,
"Anguilla"=>Anguilla,
"Antigua an d Barbuda"=>Antigua an d Barbuda,
"Argentina"=>Argentina,
As the number of countries are a lot, how can i write a function to do the job for me?
thanks for your help
shaneiadt
Forum Newbie
Posts: 10
Joined: Sat Mar 15, 2008 9:26 pm

Re: need a function

Post by shaneiadt »

Use a for loop to go through the list of country names and dynamically create each associative array element every iteration, easy :)

e.g. something like this.

Code: Select all

for($x =0; $x != count($countryNamesArray); $x++){
       $newAssoArray[$countryNamesArray[$x]] = $countryNamesArray;
}
if your array is like below;
""=>Armenia,
""=>Aruba,
""=>Australia,
""=>Austria,
""=>Azerbaijan,
""=>Bahamas,
""=>Bahrain,
""=>Bangladesh,
""=>Barbados,
This would essentially just be a normal array???;

Code: Select all

array("Aruba","Australia"...etc);
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need a function

Post by Stryks »

Fair call really. How ARE you getting these values? As an array, or are you reading them from a file or similar?

That code by shaneiadt would work for an array, as would the following.

Code: Select all

foreach($countryNamesArray as $value) $newAssoArray[$value] = $value;
I just find foreach() much easier to read. But then, neither method is going to be much immediate help if you are importing for a text file.

Cheers
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Re: need a function

Post by m2babaey »

but they are in a text file
in fact, i looked at the source code of a webpage that requested country selection on sign up to use in my own site.
i got the list of countries that way and now i need to put them in an array
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need a function

Post by Stryks »

OK, well then that needs a different solution ... Let's see.

What would you want to do with the data? Update the file with the new information, or use it in your app somehow?
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Re: need a function

Post by m2babaey »

i want to make an array of countries to add a select field in my form. i just need that array in php
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need a function

Post by Stryks »

I'm not sure how I feel about putting together a solution for you ... the link in your sig seems to indicate that I'd potentially be doing work that you would be getting paid for. :roll:

So .. lets just say that one way to handle it would involve get_file_contents(), explode(), substr() and trim(), with a touch of array_combine() thrown in for good luck.

And that just one way ... I can think of a few variations on that theme.

Either way, once you imported it, I'd either store the list in a database, or dump it back to a text file as CSV or similar for easy import later.

Cheers
Post Reply