Symfony2.4.0 formbuilder 2d array values - php

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));

Related

How to call php return function in cakephp 3 template view?

I'm not very familiar with this type of function (return without parameter).
function get_countries()
{
return [
"AF" => __("Afganistan"),
"AL" => __("Albania"),
"DZ" => __("Algeria"),
"AS" => __("American Samoa"),
"AD" => __("Andorra"),
"AO" => __("Angola"),
"AI" => __("Anguilla"),
"AQ" => __("Antarctica"),
"AG" => __("Antigua and Barbuda"),
"AR" => __("Argentina"),
"AM" => __("Armenia"),
"AW" => __("Aruba"),
"AU" => __("Australia"),
"AT" => __("Austria"),
"AX" => __("Åland Islands")
];
}
How would I call this function in Cakephp template view?
Say I have 'AF' as a value to call this function in order to get 'Afganistan'.
The easiest way to get one of the elements by key would be this:
function get_country($country)
{
return [
"AF" => __("Afganistan"),
"AL" => __("Albania"),
"DZ" => __("Algeria"),
"AS" => __("American Samoa"),
"AD" => __("Andorra"),
"AO" => __("Angola"),
"AI" => __("Anguilla"),
"AQ" => __("Antarctica"),
"AG" => __("Antigua and Barbuda"),
"AR" => __("Argentina"),
"AM" => __("Armenia"),
"AW" => __("Aruba"),
"AU" => __("Australia"),
"AT" => __("Austria"),
"AX" => __("Åland Islands")
][$country];
}
If you want to have your code a bit more clean and reusable, split keep your function as posted and add another one:
function get_country($country)
{
return get_countries()[$country];
}
You may also write return get_countries()[$country] ?? '' if you cannot be sure that there is a country for each input value. It will simply return an empty string then.

Speech Synthesize API no longer working properly

The speech synthesizer API seems to be only working for English. Is it just me? Am I doing something wrong in the code? Or is it not working for anyone else? For some languages like Chinese, and Japanese, it doesn't output any sounds at all, and for languages like Spanish and others, it just reads them like how it reads the regular English language. This just started happening recently, maybe for the past 2 weeks or so? Before the same exact code worked fine.
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(function() {
//te amo <--- spanish
// 我爱你 <--- chinese
var speech = "我爱你";
document.write(speech);
var u = new SpeechSynthesisUtterance(speech);
u.lang = "zh-CN";
u.volume = 1; //0-1 1=default
u.rate = 0.5; //0-10, 1=default
u.pitch = 1.9; //0-2 1=default
window.speechSynthesis.speak(u);
});
</script>
For Reference:
http://blog.teamtreehouse.com/getting-started-speech-synthesis-api
"arabic" => "ar-DZ",
"bulgarian" => "bg",
"catalan" => "ca",
"chinese-simplified" => "zh-CN",
"chinese-traditional" => "zh-CN",
"czech" => "cs",
"danish" => "da",
"dutch" => "nl-BE",
"english" => "en-US",
"estonian" => "et",
"finnish" => "fi",
"french" => "fr-BE",
"german" => "de-AT",
"greek" => "el",
"haitian-creole" => "en-US",
"hebrew" => "he",
"hindi" => "hi",
"hungarian" => "hu",
"indonesian" => "id",
"italian" => "it-IT",
"japanese" => "ja",
"klingon" => "en-US",
"korean" => "ko",
"latvian" => "lv",
"lithuanian" => "lt",
"malay" => "ms-BN",
"maltese" => "mt",
"norwegian" => "no-NO",
"persian" => "fa",
"polish" => "pl",
"portuguese" => "pt-BR",
"romanian" => "ro-MO",
"russian" => "ru",
"slovak" => "sk",
"slovenian" => "sl",
"spanish" => "es-AR",
"swedish" => "sv-FI",
"thai" => "th",
"turkish" => "tr",
"ukrainian" => "uk",
"urdu" => "ur",
"vietnamese" => "vi",
"welsh" => "cy",

Looping through an Multidimentinal/Associative Array

Looking for some help with looping through a multidimensional/associative array in PHP. Essentially, I need to loop through and output the certain key values in a table. Not having much luck.
Here is a sample of my array.
$myarray = array(
"body" => array(
"0" => array(
"machine" => array(
"id" => "1",
"name" => "macbookpro",
"description" => "laptop machine",
"state" => "reserved",
"state_since" => "2013-08-28 12:05:00",
"queue_snapshot" => array(
"time" => "2013-08-01 12:00:00",
"jobs" => "450",
"jobs_available" => "90",
"cputime_running" => "00:01:00",
"cputime_eligible" => "00:90:00",
"cputime_block" => " 90:00:00",
)
)
),
"1" => array(
"machine" => array(
"id" => "2",
"name" => "ipad",
"description" => "tablet machine",
"state" => "available",
"state_since" => "2013-08-28 12:05:00",
"queue_snapshot" => array(
"time" => "2013-08-01 12:00:00",
"jobs" => "50",
"jobs_available" => "20",
"cputime_running" => "00:05:00",
"cputime_eligible" => "00:12:00",
"cputime_block" => " 00:10:00",
)
)
)
));
I have some values in this array that will need to be accessed later so I only need to be able to access particular values in order to create this table. Needs to output like this....
Machine Name | Description | State | Jobs | Jobs Available |
macbookpro laptop machine reserved 450 90
ipad tablet machine available 50 20
Untestet on the fly
<?php
if(isset($myarray['body']) && is_array($myarray['body']))
foreach($myarray['body'] as $id=>$arr) {
$name = $arr['name'];
$description = $arr['description'];
$jobs = $arr['queue_snapshot']['jobs'];
$jobs_available = $arr['queue_snapshot']['jobs_available'];
echo "<br>$name $description $jobs $jobs_available";
}
?>

making sure a generated array key is unique

I have this array
$array = array(
"one" => "bar",
"two" => "21",
"three" => "22",
"four" => "25",
"five" => array(
"xxxxxxxxxxxxxxxxxx" => array(
"ar1" => "food",
"ar2" => "dr",
"ar3" => "ch",
"ar4" => "ju"
),
"yyyyyyyyyyyyyyyyyyyy" => array(
"ar1" => "food",
"ar2" => "dr",
"ar3" => "ch",
"ar4" => "ju"
)),
"six" => "et",
"seven" => "op",
"eight" => "hjs",
"nine" => array(
"1" => array(
"ar5" => "food",
"ar87" => "dr",
"ar21" => "ch",
"ar443" => "ju"
),
"2" => array(
"73" => "food",
"82" => "dr",
"90" => "ch",
"2123" => "ju"
)),
"ten" => "bar",
"eleven" => "bar",
"twelve" => "bar"
);
and i want to make sure that every new array added must have a unique array key.To do that,i first get all the keys of the others i.e $array['nine'] in my case then run a function until a unique key is found.
To add a new array to $array['nine'] and to get all the keys in nine
$keyedd = array_keys($array['nine']);
$appendThis[''.unique_array_key($keyedd).''] = array(
"73" => "nitrous",
"82" => "oxide",
"90" => "laughing",
"2123" => "gas"
);
$array['nine'] = $array['nine'] + $appendThis;
This is the function
function unique_array_key($the_array){
$r = rand();
if (array_key_exists("$r", $the_array)) {
unique_array_key($the_array);
}
else if(!array_key_exists("$r", $the_array)) {
echo $r;
}
}
Once i run this
[nine] => Array
(
[1] => Array
(
[ar5] => food
[ar87] => dr
[ar21] => ch
[ar443] => ju
)
[2] => Array
(
[73] => food
[82] => dr
[90] => ch
[2123] => ju
)
[] => Array
(
[73] => nitrous
[82] => oxide
[90] => laughing
[2123] => gas
)
)
the newly added array do not have an array key.However,unique_array_key($keyedd); on its own seems to work fine.Why is the function not adding a unique key?.
I would prefer internal PHP functionality for unique key generation:
$appendThis[uniqid()] = array(
"73" => "nitrous",
"82" => "oxide",
"90" => "laughing",
"2123" => "gas"
);
No needs to reinvent wheel. Check documentation for uniqid() function.
Update
If you have web-cluster running your application simultaneously on several hosts with shared data storage, and more than 10000 ID generations in one second just use:
uniqid(php_uname('n'), true)
for ID generation. Your solution with rand() ID generation have only mt_getrandmax() variations. Usually it is 2147483647. With 10000 ID generations in sec you'll get exceed available ID's limit in 2.4 days, and your code will run in infinite loop because every key check in array will have true result and recursively call new ID generation again and again.
Also, your solution make the application performance dependent on data amount. Than more data you'll have, than higher chance of additional loops for ID generation because of duplicates. For example, if you'll have N already stored arrays with unique ID's, there is a chance next ID will be generated for N+1 cycles.
If your project not highload, will be enough uniqid() or uniqid('', true) if you need additional entropy.
Replace echo $r; with return $r;:
function unique_array_key($the_array){
$r = rand();
if (array_key_exists("$r", $the_array)) {
return unique_array_key($the_array);
} else {
return $r;
}
}
}

PHP array_key_exists does not work; array is not multi-dimensional

I have an array of all the countries in the world as such:
$countries = array(
"GB" => "United Kingdom",
"US" => "United States",
"AF" => "Afghanistan",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AO" => "Angola",
"AI" => "Anguilla",
"AQ" => "Antarctica",
"AG" => "Antigua And Barbuda",
"AR" => "Argentina",
"AM" => "Armenia",
"AW" => "Aruba",
"AU" => "Australia",
"AT" => "Austria",
"AZ" => "Azerbaijan",
"BS" => "Bahamas",
"BH" => "Bahrain",
"BD" => "Bangladesh",
"BB" => "Barbados",
"BY" => "Belarus",
"BE" => "Belgium",
"BZ" => "Belize",
"BJ" => "Benin",
"BM" => "Bermuda",
"BT" => "Bhutan",
"BO" => "Bolivia",
"BA" => "Bosnia And Herzegowina",
"BW" => "Botswana",
"BV" => "Bouvet Island",);
And so on for all countries; I am 100% positive every country is listed properly.
I have an application form which stores the result in a file stored on the server. Currently the review page for the application is a basic text version and I am now in the process of putting it into a mock-form for my client to have a more visually appealing method of reviewing applications.
So an array named $in_data stores the results that come from the file. This array is structured as such "emergency_medical_insurance" => "value_user_entered". Each key is the name of the HTML element it came form and the value is what the user put in.
The country select list on the form returns a two-letter code of the country. So what I am trying to do is search $countries with the value of $in_data['country_select'] and then return the name of the country.
echo $in_data['country_select']; returns 'CA' the letter code for Canada and the test country I have entered.
echo $countries['CA']; returns 'Canada'
if (array_key_exists($in_data['country_select'], $countries)){
echo "Country Found";
}
else { echo "failed"; }
returns nothing.
if (array_key_exists('CA', $countries)){
echo "Country Found";
}
else { echo "failed"; }
Also returns nothing. And when I say nothing I mean nothing, not null, not true, not false; just doesn't even run.
My question is simple; how is the code below (taken from the offical PHP manual) which does EXACTLY the same thing my code does, working, but my code won't even return anything?
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
since you are reading from a file, you may be getting other characters, try trim():
if (array_key_exists(trim($in_data['country_select']), $countries)){
echo "Country Found";
}
else { echo "failed"; }
I had a similar problem while reading from a file.
The solution was to remove the BOM from the first line.
function remove_utf8_bom($text) {
$bom = pack('H*','EFBBBF');
$text = preg_replace("/$bom/", '', $text);
return $text;
}

Categories