1

I have a text file countries.txt with a list of all countries:

1. Australia
2. Austria
3. Belgium
4. US

I want to create an array in country_handler.php like this:

$countries = array(
     '1'=>'Australia',
     '2'=>'Austria',
     '3'=>'Belgium',
     '4'=>'US'
); 

How to do that?

6 Answers 6

1

Better you can use this in a function like below - then just called this function from any where

function getCountryList() { return array( '1'=>'Australia', '2'=>'Austria', '3'=>'Belgium', '4'=>'US' ); }

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your effort, but there are 250+ countries in my txt file, and it is hard to retype it again. I also have a 250+ txt files with cities for every country and hard coding would be time consuming.
i feel better you store this data in db table.its too complex to maintain using array with different file.
I have been thinking about using DB for countries, and it would be easy. The problem is how to store cities in DB. There would be 25000 entries in DB. I had two options: 1. for every country one table (pros:easy to maintain, cons:250 tables and 250 queries to get all cities). 2. all cities in one table (pros:easy to get, cons:hard to maintain)
yes like this ..in country table here you store all country list.then in other table name city store the all data using country table foreign key. first time its too pain for you but its works lifetime for you :)
0
$data = file_get_contents('countries.txt');
$countries = explode(PHP_EOL, $data);

2 Comments

That was a fast answer! :) Thank you
Doesn't do anything with keys. PHP's EOL might not be the same as the file's.
0
$file = file('countries.txt');

$countries = array();

foreach ($file as $line) {
    @list ($index, $country) = explode('.', $line);
    $countries[trim($index)] = trim($country);
}

print_r($countries);

Comments

0
$lines = file('countries.txt');

$newArr = array();
foreach ($lines as $line) {
    $erg = preg_split('/\.\s+/', $line);
    $newArr[$erg[0]] = $erg[1];  
}
var_dump($newArr);

2 Comments

Does it work for The Netherlands and Czech Republic as well?
Yes, because of the "."
0

Luckily (sometimes) regex doesn't include newlines in the ., so you can do this:

$contents = file_get_contents('countries.txt');
preg_match_all('#(\d+)\.\s+(.+)#', $contents, $matches);
print_r($matches);

The regex means:

  • (\d+) -- a number (save this)
  • \. -- a literal dot
  • \s+ -- white space
  • (.+) -- any character except \n or \r (end-of-line) (save this)

Which results in:

Array
(
    [0] => Array
        (
            [0] => 1. Australia
            [1] => 2. Austria
            [2] => 3. Belgium
            [3] => 4. US
        )

    [1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [2] => Array
        (
            [0] => Australia
            [1] => Austria
            [2] => Belgium
            [3] => US
        )

)

and I'm sure from [1] and [2] you can make the keyed array you need.

array_combine to the easy rescue:

$countries = array_combine($matches[1], $matches[2]);

Comments

0

Try this:

<?php
    $data = file_get_contents('countries.txt');
    $countries = explode(PHP_EOL,$data);               
    $cnt = 1;
    foreach($countries as $val) {
      if($val <>"") {
         $res[$cnt] = strstr(trim($val), ' ');
         $cnt++;
      }
    }
    print_r($res);
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.