PHP: Linking an array value with a variable - php

I have an array that stores names of countries (keys) and age expectancy (values).
$countries = array(
'Costa Rica' => 78, 'Puerto Rico' => 78, 'Colombia' => 73,
'Nicaragua' => 73, 'El Salvador' => 72, 'Peru' => 71,
'Argentina' => 75, 'Uruguay' => 76, 'Ecuador' => 75,
'Mexico' => 76, 'Venezuela' => 73, 'Brasil' => 72,
);
An html form creates a new variable ($country) with a name that corresponds to one countries listed above.
For example: "Costa Rica"
How can I return a variable with the number (age expectancy) that matches the name of the country in the $country variable?
Take for example:
Costa Rica = 78
$ageExpectancy = 78;
Thanks, I hope to be concise.

<?php
$country = $_POST['country']; // assuming post method and form element named 'country'
$ageExpectancy = $countries[$country];
?>

Related

Symfony2.4.0 formbuilder 2d array values

I have an array like this.
array(
1 => array("United States","Antigua and Barbuda","Anguilla","American Samoa","Barbados","Bermuda","Bahamas","Canada","Dominica","Dominican Republic","Grenada","Guam","Jamaica","Saint Kitts and Nevis","Cayman Islands","Saint Lucia","Northern Mariana Islands","Montserrat","Puerto Rico","Sint Maarten (Dutch part)","Turks and Caicos Islands","Trinidad and Tobago","Saint Vincent and the Grenadines","Virgin Islands, U.S.","Virgin Islands, U.S.")
,7 => array("Russian Federation","Kazakhstan")
,20 => array("Egypt")
,27 => array("South Africa")
,30 => array("Greece")
,31 => array("Netherlands")
,32 => array("Belgium")
,33 => array("France")
,34 => array("Spain")
,36 => array("Hungary")
,39 => array("Italy")
,40 => array("China")
,41 => array("Switzerland")
,43 => array("Austria")
,44 => array("United Kingdom","Guernsey","Isle of Man","Jersey")
,45 => array("Denmark")
,46 => array("Sweden")
,47 => array("Norway","Svalbard and Jan Mayen")
,48 => array("Poland")
)
and I would like it such that the values in the html select element have a value that is the same as the parent key. Right now I'm using
$builder->add('callingCode', 'choice', array(
'choices' => $thatarray));
but that results in optgroups like this.
1
United States
Antigua and Barbuda
Anguilla
...
7
Russian Federation
Kazakhstan
20
Egypt
...
United States has a value of 0, Antigua and Barbuda has a value of 1, Anguilla has a value of 2, Russian Federation has a value of 0, etc. That is not what I want. United States, Antigua and Barbuda, and Anguilla should have a value of 1. Russian Federation, and Kazahkstan should have a value of 7, and Egypt should have a value of 20. Is such a thing possible?
You need to make a single array with key values
$thatarray=array(
"1" => "United States"
"1" =>"Antigua and Barbuda",
"1" =>"Anguilla",
"1" =>"American Samoa",
"1" =>"Barbados",
"7" => "Russian Federation",
"7" =>"Kazakhstan",
"20" => "Egypt",
./*other countries */
.
.
.
.
);
$builder->add('callingCode', 'choice', array('choices' => $thatarray));

Get key of max element in array [duplicate]

This question already has answers here:
Return index of highest value in an array
(8 answers)
Closed 9 years ago.
I have an array:
$someArray = array('fb' => 32, 'gp' => 11, 'tw' => 7, 'vk' => 89, 'ok' => 112);
As you can see last element in array has the greatest value. I need to return the key(ok) of last element. How to do this?
Based on https://stackoverflow.com/a/1461363/1641835:
$someArray = array('fb' => 32, 'gp' => 11, 'tw' => 7, 'vk' => 89, 'ok' => 112);
$max_keys = array_keys($someArray, max($someArray));
// $max_keys would now be an array: [ 'ok' ]
$max_keys will now be an array of all the keys that point to the maximum value. If you know there will only be one, or you don't care which you retrieve, you could instead use:
$someArray = array('fb' => 32, 'gp' => 11, 'tw' => 7, 'vk' => 89, 'ok' => 112);
$max_key = array_search(max($someArray), $someArray);
// $max_key would now be 'ok'

explode mysql string for $wpdb->insert

I'm trying to explode this string, so it can fit "$wpdb->insert" function in wordpress.
this is the string to insert. The file contains about 42k strings like this:
(601, 'STANDARD', 'Adjuntas', '', 'Colinas Del Gigante, Jard De Adjuntas, Urb San Joaquin', 'PR', 'Adjuntas', 'America/Puerto_Rico', '787,939', '18.16', '-66.72', 'NA', 'US', 0, 0, ''),
this is the code
if ( file_exists(realpath(dirname(__FILE__)).'/install/usa_zip_codes.sql')){
$filecontent = explode("\n",file_get_contents( realpath(dirname(__FILE__)).'/install/usa_zip_codes.sql'));
foreach( $filecontent as $row){
$array = array();
$array = explode(", ", trim(preg_replace("/[()]/","",$row),","));
$wpdb->insert(
$wpmyplugin_table['yp_usa_zip_codes'],
array(
'zip' => $array[0],
'type' => trim($array[1],"'"),
'primary_city' => trim($array[2],"'"),
'acceptable_cities' => trim($array[3],"'"),
'unacceptable_cities' => trim($array[4],"'"),
'state' => trim($array[5],"'"),
....
but when i have strings like 'Colinas Del Gigante, Jard De Adjuntas, Urb San Joaquin', it will also split it to array elements. I've tried to explode by explode(", '", but this part , 0, 0, '' wont be split correctly. How do you deal with these things?
Have a look at str_getcsv. You should be able to convert
(601, 'STANDARD', 'Adjuntas', '', 'Colinas Del Gigante, Jard De Adjuntas, Urb San Joaquin', 'PR', 'Adjuntas', 'America/Puerto_Rico', '787,939', '18.16', '-66.72', 'NA', 'US', 0, 0, ''),
Into:
601, 'STANDARD', 'Adjuntas', '', 'Colinas Del Gigante, Jard De Adjuntas, Urb San Joaquin', 'PR', 'Adjuntas', 'America/Puerto_Rico', '787,939', '18.16', '-66.72', 'NA', 'US', 0, 0, ''
str_getcsv can then be used to turn the above string into an array. This will ignore commas inside of values.

Fill in the blanks in an array

I have an array which I'd like to show all days of weeks or each month of the year, even if data is 0, is it possible to look inside the array and fill in what's not there?
The data I'm returning from mysql table does not show 0 - details below
Show values even if empty
So, the following shows months of the year and the count, I'd like to fill in the months which aren't there with i.e. 'Feb' => '0' .. 'Sep' => '0' .. 'Dec' => '0'
Array example:
$data = array(
'Jan' => 12,
'Mar' => 10,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Oct' => 23,
'Nov' => 78,
);
Use array_fill_keys to create a "known good starting point" and then array_merge or array addition to incorporate your data.
Example:
$data = array_fill_keys(array('Jan', 'Feb', 'etc'), 0);
$data = array('Feb' => 40) + $data;
Caveat: the result will not end up being ordered by month.
First of all create a blank array with default 0 value than merge this array to your original array.
$data = array(
'Jan' => 0,
'Feb' => 0,
'Mar' => 0,
'Apr' => 0,
'May' => 0,
'Jun' => 0,
'Jul' => 0,
'Aug' => 0,
'Sep' => 0,
'Oct' => 0,
'Nov' => 0,
'Dec' => 0;
);
$data2 = array(
'Jan' => 12,
'Mar' => 10,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Oct' => 23,
'Nov' => 78,
);
$newarray=array_merge($data, $data2);
As already pointed out in the previous question, you need create some kind of "lookup table" for the month names. If you do not do that within the database, you need to do it in PHP. Such a "table" could be an array of month names:
$months = array(
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
);
You can then iterate over the month names an pick the number if it exists in the result or you take the default value:
$default = 0;
foreach ($months as $month) {
$value = isset($data[$month]) ? $data[$month] : $default;
# $value now contains the month value
...
}
This should just work.
Take care that the month names that the database returns need to be the same you use in your $months array.

php table of values

I have a table of values like this:
http://www.conversiontable.org/clothingsizeconversiontable.html
and I want to declare these values in a php class and then manipulate them easily.
In your opinion, what is the best way to do such thing?
You can build an array indexed by country name and then by size number:
$country_sizes = array(
'United States' => array(6, 8, 10, 12, 14, 16, 18),
...
)
But in order to avoid mistakes and make the code more readable, I would assign a label to each size type. Then I would build an array indexed by size type first, and then by country:
$sizes = array(
'S' => array(
'United States' => 6,
'United Kingdom' => 28,
...
),
'M' => array(
'United States' => 8,
'United Kingdom' => 30,
...
),
...
);
This second way is more tedious to build, but seems more natural to me (what's the S size in U.K.?). Anyway, it's your choice according to your needs ;)
something like:
$sizes = array(
'United States' => array(6, 8, 10, 12, 14, 16, 18),
'United Kingdom' => array (28, 30, 32, 34, 36, 38, 40)
);
echo('United states first size: ' . $sizes['United States'][0]);

Categories