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
need a function
Moderator: General Moderators
Re: need a function
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.
if your array is like below;
e.g. something like this.
Code: Select all
for($x =0; $x != count($countryNamesArray); $x++){
$newAssoArray[$countryNamesArray[$x]] = $countryNamesArray;
}This would essentially just be a normal array???;""=>Armenia,
""=>Aruba,
""=>Australia,
""=>Austria,
""=>Azerbaijan,
""=>Bahamas,
""=>Bahrain,
""=>Bangladesh,
""=>Barbados,
Code: Select all
array("Aruba","Australia"...etc);Re: need a function
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.
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
That code by shaneiadt would work for an array, as would the following.
Code: Select all
foreach($countryNamesArray as $value) $newAssoArray[$value] = $value;Cheers
Re: need a function
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
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
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?
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
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
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
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