Page 1 of 1

Out of one array I want to make three

Posted: Tue Jan 17, 2012 11:20 am
by lovelf
Out of the following array:

Code: Select all

 'Countries' => array(
'BKQ ' => 'Blackall, Australia - Blackall (BKQ )',
'BLK ' => 'Blackpool, United Kingdom - Blackpool Airport (BLK )',
'BCB ' => 'Blacksburg, United States - Virginia Tech (BCB )',
'BKT ' => 'Blackstone, United States - Blackstone AAF (BKT )',
'BLT ' => 'Blackwater, Australia - Blackwater (BLT )',
'BWL ' => 'Blackwell, United States - Blackwell (BWL )'
)
I want to have three arrays, one for Australia, one for the United Kingdom and one for the United States, what function should I make?

Thanks.

Re: Out of one array I want to make three

Posted: Tue Jan 17, 2012 2:44 pm
by twinedev
You will need to use preg_match.

For the data given, assuming you will always have that format, the something along the lines of:

Code: Select all

<?php

$aryTest =  array(
'BKQ' => 'Blackall, Australia - Blackall (BKQ )',
'BLK' => 'Blackpool, United Kingdom - Blackpool Airport (BLK )',
'BCB' => 'Blacksburg, United States - Virginia Tech (BCB )',
'BKT' => 'Blackstone, United States - Blackstone AAF (BKT )',
'BLT' => 'Blackwater, Australia - Blackwater (BLT )',
'BWL' => 'Blackwell, United States - Blackwell (BWL )'
);

$aryByCountry = array();

foreach($aryTest as $key=>$val) {
	if (preg_match('/^.+, (.+) - .+$/',$val,$regs)) {
		$strCountry = $regs[1];
		if (!isset($aryByCountry[$strCountry])) { $aryByCountry[$strCountry] = array(); }
		$aryByCountry[$strCountry][$key] = $val;
	}
}

var_dump($aryByCountry);
gives you:

Code: Select all

array(3) {
  ["Australia"]=>
  array(2) {
    ["BKQ"]=>
    string(37) "Blackall, Australia - Blackall (BKQ )"
    ["BLT"]=>
    string(41) "Blackwater, Australia - Blackwater (BLT )"
  }
  ["United Kingdom"]=>
  array(1) {
    ["BLK"]=>
    string(52) "Blackpool, United Kingdom - Blackpool Airport (BLK )"
  }
  ["United States"]=>
  array(3) {
    ["BCB"]=>
    string(48) "Blacksburg, United States - Virginia Tech (BCB )"
    ["BKT"]=>
    string(49) "Blackstone, United States - Blackstone AAF (BKT )"
    ["BWL"]=>
    string(43) "Blackwell, United States - Blackwell (BWL )"
  }
}

Re: Out of one array I want to make three

Posted: Wed Jan 18, 2012 5:56 am
by lovelf
OK. It worked, but as I have it wrongly formatted, with over 9000 entries and after the comma there are pieces of the city name along the country name I get over 1500 arrays which is too many for 260 or so countries, so I made an array with the countries listed as arrays.

Code: Select all

<?php
$aCountries=array(
'Afghanistan' => array(), 
'Akrotiri' => array(), 
'Albania' => array(), 
'Algeria' => array(), 
'American Samoa' => array(), 
'Andorra' => array(), 
'Angola' => array(), 
'Anguilla' => array(), 
'Antarctica' => array(), 
'Antigua and Barbuda' => array(), 
'Argentina' => array(), 
'Armenia' => array(), 
'Aruba' => array(), 
'Ashmore and Cartier Islands' => array(), 
'Australia' => array(), 
'Austria' => array(), 
'Azerbaijan' => array(), 
'Bahamas, The' => array(), 
'Bahrain' => array(), 
'Bangladesh' => array(), 
'Barbados' => array(), 
'Bassas da India' => array(), 
'Belarus' => array(), 
'Belgium' => array(), 
'Belize' => array(), 
'Benin' => array(), 
'Bermuda' => array(), 
'Bhutan' => array(), 
'Bolivia' => array(), 
'Bosnia and Herzegovina' => array(), 
'Botswana' => array(), 
'Bouvet Island' => array(), 
'Brazil' => array(), 
'British Indian Ocean Territory' => array(), 
'British Virgin Islands' => array(), 
'Brunei' => array(), 
'Bulgaria' => array(), 
'Burkina Faso' => array(), 
'Burma' => array(), 
'Burundi' => array(), 
'Cambodia' => array(), 
'Cameroon' => array(), 
'Canada' => array(), 
'Cape Verde' => array(), 
'Cayman Islands' => array(), 
'Central African Republic' => array(), 
'Chad' => array(), 
'Chile' => array(), 
'China' => array(), 
'Christmas Island' => array(), 
'Clipperton Island' => array(), 
'Cocos (Keeling) Islands' => array(), 
'Colombia' => array(), 
'Comoros' => array(), 
'Congo, Democratic Republic of the' => array(), 
'Congo, Republic of the' => array(), 
'Cook Islands' => array(), 
'Coral Sea Islands' => array(), 
'Costa Rica' => array(), 
'Cote d Ivoire' => array(), 
'Croatia' => array(), 
'Cuba' => array(), 
'Cyprus' => array(), 
'Czech Republic' => array(), 
'Denmark' => array(), 
'Dhekelia' => array(), 
'Djibouti' => array(), 
'Dominica' => array(), 
'Dominican Republic' => array(), 
'Ecuador' => array(), 
'Egypt' => array(), 
'El Salvador' => array(), 
'Equatorial Guinea' => array(), 
'Eritrea' => array(), 
'Estonia' => array(), 
'Ethiopia' => array(), 
'Europa Island' => array(), 
'Falkland Islands (Islas Malvinas)' => array(), 
'Faroe Islands' => array(), 
'Fiji' => array(), 
'Finland' => array(), 
'France' => array(), 
'French Guiana' => array(), 
'French Polynesia' => array(), 
'French Southern and Antarctic Lands' => array(), 
'Gabon' => array(), 
'Gambia, The' => array(), 
'Gaza Strip' => array(), 
'Georgia' => array(), 
'Germany' => array(), 
'Ghana' => array(), 
'Gibraltar' => array(), 
'Glorioso Islands' => array(), 
'Greece' => array(), 
'Greenland' => array(), 
'Grenada' => array(), 
'Guadeloupe' => array(), 
'Guam' => array(), 
'Guatemala' => array(), 
'Guernsey' => array(), 
'Guinea' => array(), 
'Guinea-Bissau' => array(), 
'Guyana' => array(), 
'Haiti' => array(), 
'Heard Island and McDonald Islands' => array(), 
'Holy See (Vatican City)' => array(), 
'Honduras' => array(), 
'Hong Kong' => array(), 
'Hungary' => array(), 
'Iceland' => array(), 
'India' => array(), 
'Indonesia' => array(), 
'Iran' => array(), 
'Iraq' => array(), 
'Ireland' => array(), 
'Isle of Man' => array(), 
'Israel' => array(), 
'Italy' => array(), 
'Jamaica' => array(), 
'Jan Mayen' => array(), 
'Japan' => array(), 
'Jersey' => array(), 
'Jordan' => array(), 
'Juan de Nova Island' => array(), 
'Kazakhstan' => array(), 
'Kenya' => array(), 
'Kiribati' => array(), 
'Korea, North' => array(), 
'Korea, South' => array(), 
'Kuwait' => array(), 
'Kyrgyzstan' => array(), 
'Laos' => array(), 
'Latvia' => array(), 
'Lebanon' => array(), 
'Lesotho' => array(), 
'Liberia' => array(), 
'Libya' => array(), 
'Liechtenstein' => array(), 
'Lithuania' => array(), 
'Luxembourg' => array(), 
'Macau' => array(), 
'Macedonia' => array(), 
'Madagascar' => array(), 
'Malawi' => array(), 
'Malaysia' => array(), 
'Maldives' => array(), 
'Mali' => array(), 
'Malta' => array(), 
'Marshall Islands' => array(), 
'Martinique' => array(), 
'Mauritania' => array(), 
'Mauritius' => array(), 
'Mayotte' => array(), 
'Mexico' => array(), 
'Micronesia, Federated States of' => array(), 
'Moldova' => array(), 
'Monaco' => array(), 
'Mongolia' => array(), 
'Montserrat' => array(), 
'Morocco' => array(), 
'Mozambique' => array(), 
'Namibia' => array(), 
'Nauru' => array(), 
'Navassa Island' => array(), 
'Nepal' => array(), 
'Netherlands' => array(), 
'Netherlands Antilles' => array(), 
'New Caledonia' => array(), 
'New Zealand' => array(), 
'Nicaragua' => array(), 
'Niger' => array(), 
'Nigeria' => array(), 
'Niue' => array(), 
'Norfolk Island' => array(), 
'Northern Mariana Islands' => array(), 
'Norway' => array(), 
'Oman' => array(), 
'Pakistan' => array(), 
'Palau' => array(), 
'Panama' => array(), 
'Papua New Guinea' => array(), 
'Paracel Islands' => array(), 
'Paraguay' => array(), 
'Peru' => array(), 
'Philippines' => array(), 
'Pitcairn Islands' => array(), 
'Poland' => array(), 
'Portugal' => array(), 
'Puerto Rico' => array(), 
'Qatar' => array(), 
'Reunion' => array(), 
'Romania' => array(), 
'Russia' => array(), 
'Rwanda' => array(), 
'Saint Helena' => array(), 
'Saint Kitts and Nevis' => array(), 
'Saint Lucia' => array(), 
'Saint Pierre and Miquelon' => array(), 
'Saint Vincent and the Grenadines' => array(), 
'Samoa' => array(), 
'San Marino' => array(), 
'Sao Tome and Principe' => array(), 
'Saudi Arabia' => array(), 
'Senegal' => array(), 
'Serbia and Montenegro' => array(), 
'Seychelles' => array(), 
'Sierra Leone' => array(), 
'Singapore' => array(), 
'Slovakia' => array(), 
'Slovenia' => array(), 
'Solomon Islands' => array(), 
'Somalia' => array(), 
'South Africa' => array(), 
'South Georgia and the South Sandwich Islands' => array(), 
'Spain' => array(), 
'Spratly Islands' => array(), 
'Sri Lanka' => array(), 
'Sudan' => array(), 
'Suriname' => array(), 
'Svalbard' => array(), 
'Swaziland' => array(), 
'Sweden' => array(), 
'Switzerland' => array(), 
'Syria' => array(), 
'Taiwan' => array(), 
'Tajikistan' => array(), 
'Tanzania' => array(), 
'Thailand' => array(), 
'Timor-Leste' => array(), 
'Togo' => array(), 
'Tokelau' => array(), 
'Tonga' => array(), 
'Trinidad and Tobago' => array(), 
'Tromelin Island' => array(), 
'Tunisia' => array(), 
'Turkey' => array(), 
'Turkmenistan' => array(), 
'Turks and Caicos Islands' => array(), 
'Tuvalu' => array(), 
'Uganda' => array(), 
'Ukraine' => array(), 
'United Arab Emirates' => array(), 
'United Kingdom' => array(), 
'United States' => array(), 
'Uruguay' => array(), 
'Uzbekistan' => array(), 
'Vanuatu' => array(), 
'Venezuela' => array(), 
'Vietnam' => array(), 
'Virgin Islands' => array(), 
'Wake Island' => array(), 
'Wallis and Futuna' => array(), 
'West Bank' => array(), 
'Western Sahara' => array(), 
'Yemen' => array(), 
'Zambia' => array(), 
'Zimbabwe' => array()
);

?>
Now I have those arrays and the data should be entered to each one of them and then all of them printed out with print_r($aCountries);

Hot to search for inside the first array of data for an array named with the country name to put in the airport key

'BWL' => 'Blackwell, United States - Blackwell (BWL )'

Something should look up for United States inside that key and place this key within the United States array, the same for all of the other countries, any help appreciated.

Thanks.

Re: Out of one array I want to make three

Posted: Wed Jan 18, 2012 7:18 am
by lovelf
Alright, think I got it.

Code: Select all

<?php

foreach($aCountries as $key2=>$val2){

foreach($aryTest as $key=>$val) {


$test=str_replace($key2,'',$val,$count);
if($count>=1){
//missing code here
echo $key2.' '.$key.' '.$val."\r\n";
}
}


}
?>
I'm counting the occurrences of each array name on each key and determining whether it was found or not, I don't know the proper syntax to get into the countries arrays the airport values.
This is doing it, I get an output similar to:

Code: Select all

Afghanistan KBL Kabul, Afghanistan - Khwaja Rawash (KBL)
Afghanistan KDH Kandahar, Afghanistan - Kandahar (KDH)
Afghanistan KHT Khost, Afghanistan - Khost (KHT)
Afghanistan KWH Khwahan, Afghanistan - Khwahan (KWH)
Afghanistan UND Kunduz, Afghanistan - Kunduz (UND)
So it's actually capable of composing the arrays, don't know what the syntax would be to compose the arrays, have tried with $key2[$key]=$val but that doesn't do it.

Re: Out of one array I want to make three

Posted: Wed Jan 18, 2012 8:37 am
by twinedev
Hmm, I see what you mean with your country list. So if you have no other method getting the list of airports, then yes, searching will probably be your best bet.

HOWEVER, just going through the list of countries, you will have the problem that there can be more than one matching country (not including that what if a city also contains something that can match a country name), In specific, you have the following list of shorter country names that will match the larger ones:

Dominica ====> Dominican Republic
Georgia ====> South Georgia and the South Sandwich Islands
Guinea ====> Equatorial Guinea
Guinea ====> Guinea-Bissau
Guinea ====> Papua New Guinea
India ====> Bassas da India
India ====> British Indian Ocean Territory
Netherlands ====> Netherlands Antilles
Niger ====> Nigeria
Samoa ====> American Samoa
Virgin Islands ====> British Virgin Islands

That being said, as long as you KNOW each country name will follow a comma and then a space, and followed by a space then a hyphen, you can add those in to make sure it matches:

", " . $country . " -" instead of just $country

Now on top of that, you should reverse the order of the foreaches. Currently you are looping through each country, and then through each piece of data. This means you are looping over 2 million times based on 260 countries with 9000 pieces of data. You can minimize this with two big helpers:

1. Loop though data, then loop through country. Once you match a country, do a break; statement to stop the loop on countries.
2. Create the original array list of countries with most used ones to the top. This way it will match faster, and therefore be less loops. If you are going to be running this more than once, after the first time, use the code I give below to see which ones are most often for sure. If this is a one time thing, just put them in the order you think would be the most.

Also, you can use the strpos to match, doing str_replace the way you do works, but just not the best choice.

So all that being said, once you have the $aCountries in an order of most used ones at top, then this should work for you:

Code: Select all

foreach($aryTest as $key=>$strAirport) {
  foreach($aCountries as $strCountry=>$aList) {
    if (strpos($strAirport,', '.$strCountry.' - ')!==FALSE) {
      $aCountries[$strCountry][$key] = $strAirport;
      break;
    }
  }
}

// If you DO need $aCountries to be alphabetical:
ksort($aCountries);

print_r($aCountries);
So now you have that, and for optimizing future runs of it (but how often do you update airport lists...), use the following code, it will not only find the correct order, it will output the code you can copy and paste into your script to replace the definition of $aCountries (do a VIEW->SOURCE on the page to see it formatted properly)

Code: Select all

$aCount = array();
foreach($aCountries as $key=>$val) {
  $aCount[$key] = count($val);
}
arsort($aCount);
echo '$aCountries = array(',"\n\t'";
echo implode("'=>array(),\n\t'",array_keys($aCount));
echo "'=>array()\n);";
I have even added in the two values to test:

Code: Select all

'ST1' => 'Somoa Test1, Samoa - Test 1 (ST1 )',
'AST' => 'Somoa Test2, American Samoa - Test 2 (AST )',
and it properly put them in the correct ones.

Using the above sample data (and the two test ones I added, running the code that gives you code to set $aCountries by most used, I get:

Code: Select all

$aCountries = array(
	'United States'=>array(),
	'Australia'=>array(),
	'Samoa'=>array(),
	'American Samoa'=>array(),
	'United Kingdom'=>array(),
	'Niue'=>array(),
	'Nigeria'=>array(),
(... All the way down to...)
	'Gaza Strip'=>array(),
	'Kuwait'=>array()
);
-Greg

Re: Out of one array I want to make three

Posted: Wed Jan 18, 2012 9:09 am
by lovelf
I finally got it working!

Thanks Greg.

Re: Out of one array I want to make three

Posted: Wed Jan 18, 2012 10:00 am
by twinedev
Glad to be able to help!

BTW, just in case you go to use it somewhere else, the reason you do strpos($string,$find) !== FALSE istead of just strpos($string,$find) != FALSE is that strpos() can return 0 (if it matched on first character), so you do !== to compare that it is not just a value that can evaluate to FALSE (which 0 would), but that it is actually a boolean value of false (no match found)

-Greg