Page 1 of 1
need a function
Posted: Mon Sep 22, 2008 7:31 am
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
Re: need a function
Posted: Mon Sep 22, 2008 8:05 am
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???;
Re: need a function
Posted: Mon Sep 22, 2008 8:27 am
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
Re: need a function
Posted: Mon Sep 22, 2008 1:56 pm
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
Re: need a function
Posted: Mon Sep 22, 2008 11:38 pm
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?
Re: need a function
Posted: Tue Sep 23, 2008 12:16 am
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
Re: need a function
Posted: Tue Sep 23, 2008 6:43 am
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.
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