I want to find out how I can store small strings in an array and output them correct.
In this case I want to set a two-letter language code in an array at the top and
then output a string in that language later.
I really appreciate your help.
The following code I've made does not work but it's something like this I'm looking for:
<?php
// Set the language
$settings = array(
Language => "en"
);
// Set the strings
$locales = array(
Installed => array("en", "da"),
TheString => array("Dog", "Hund")
);
// Do some magic
$lang = $settings["Language"][0];
// Output Dog (or Hund if the language is "da")
echo $lang["TheString"];
?>
$settings = array( 'lanaguage' => 'en');
$locales = array(
'en' => array(
'dog' => 'dog'
),
'da' => array(
'dog' => 'hund'
)
);
// You don't need this, but you can get it like so:
$installed_languages = array_keys( $locales);
echo $locales[ $settings['language'] ]['dog'];
This will either output dog if $settings['language'] is en, or hund if it is da.
Related
I am trying to create the following array dynamically:
$aSettings = array( "text"=>
array( "icon_type" =>
array(
"name"=>"icon_type",
"method"=>"dropdown",
"option"=>"",
"default"=>""
),
"column" =>
array(
"name"=>"column_count",
"method"=>"dropdown",
"default"=>"1"
)
)
)
I am not sure how to declare the array into the array.
I have the following example code:
$aSettings=array();
$aSetting_type['text']=array();
$aSetting_name['icon_type']=array();
$aSetting_name['column']=array();
$aSetting_values1=array('name'=>'icon_type','method'=>'dropdown','option'=>'','default'=>'');
$aSetting_values2=array('name'=>'column_count','method'=>'dropdown','default'=>1);
I guess I am overlooking something very simple, but how do I put all these arrays into each other?
I want to be able to call a value from the array as:
$aSettings['text']['column']['name'];
Any ideas?
You could do:
$aSettings['text']['icon_type'] = $aSetting_values1;
$aSettings['text']['column'] = $aSetting_values2;
If you need it more dynamic, you could use variables like so:
$type = 'text';
$name1 = 'icon_type';
$aSettings[$type][$name1] = $aSetting_values1;
Collect the array is reverse lower to higher order, it would be easy to collect without confusions, for example
$icon_type = array(
"name"=>"icon_type",
"method"=>"dropdown",
"option"=>"",
"default"=>""
);
$column = array(
"name"=>"column_count",
"method"=>"dropdown",
"default"=>"1"
);
$text = array(
"icon_type" => $icon_type,
"column" => $column
);
$aSettings = array(
"text"=> $text
);
Once you collect array like this you can easily access any element in the array i.e. echo $aSettings['text']['column']['name'];
I have the following code for generating a new array:
$languages = array_keys(['French'=>4, 'Spanish'=>2, 'German'=>6, 'Chinese'=>8]);
function generateLanguageRules($language)
{
return ["seatsAllocatedFor$language"=>"numeric|max:300"];
}
array_map('generateLanguageRules', $languages);
//Output:
array(
0 => array(
'seatsAllocatedForFrench' => 'numeric|max:300'
),
1 => array(
'seatsAllocatedForSpanish' => 'numeric|max:300'
),
2 => array(
'seatsAllocatedForGerman' => 'numeric|max:300'
),
3 => array(
'seatsAllocatedForChinese' => 'numeric|max:300'
)
)
I'm wondering if there is an easier way to output a flat array, instead of a nested one? I'm using Laravel. Are there maybe some helper functions that could do this?
UPDATE:
One possible Laravel specific solution:
$languages = array_keys(['French'=>4, 'Spanish'=>2, 'German'=>6, 'Chinese'=>8]);
$c = new Illuminate\Support\Collection($languages);
$c->map(function ($language){
return ["seatsAllocatedFor$language"=>"numeric|max:300"];
})->collapse()->toArray();
I dont know if laravel has a built-in method for that, (haven't used it yet). But alternatively, you could use RecursiveArrayIterator in conjunction to iterator_to_array() to flatten it and assign it. Consider this example:
$languages = array_keys(['French'=>4, 'Spanish'=>2, 'German'=>6, 'Chinese'=>8]);
function generateLanguageRules($language) {
return ["seatsAllocatedFor$language"=>"numeric|max:300"];
}
$data = array_map('generateLanguageRules', $languages);
$data = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($data)));
echo "<pre>";
print_r($data);
echo "</pre>";
Sample Output:
Array
(
[seatsAllocatedForFrench] => numeric|max:300
[seatsAllocatedForSpanish] => numeric|max:300
[seatsAllocatedForGerman] => numeric|max:300
[seatsAllocatedForChinese] => numeric|max:300
)
community.
I've been looking for an answer, but not close enough to this scenario
I have a code like this (actually working fine):
array('desc'=>'Home')
It is working defining it as text (Home) but I would rather to use it as a PHP variable due multilanguage site. Using the language package I have a variable such:
$lang['HOME'] = 'Home';
So depending on the language selected, the value changes
In other words, I need the array to have the variable value as the element
array('desc'=>'variablehere')
Can anyone plese let me know what am I missing? I have tried to set it as variable, as echo and so other ways.
Thanks a lot!
Like this?
$myArray = array('desc' => $variable);
or
$myArray = array(
'desc' => $desc,
'name' => $name
);
In your case:
$lang = array('HOME' => $homeVariable);
Use a multi-dimensional array. e.g., given something like
$lang = 'en';
$key = 'desc';
The exact array structure depends on your needs, but it could either be primarily by language, or by key-to-translate:
language-based:
$translations = array(
'en' => array('desc' => 'foo'),
'fr' => array('desc' => 'bar')
);
$text_to_display = $translations['en']['desc']; // produces 'foo'
or
$translations = array(
'desc' => array(
'en' => array('desc' => 'foo'),
'fr' => array('desc' => 'bar')
)
)
$text_to_display = $translations['desc']['fr']; // produces 'bar'
Use a translate function instead:
// It can be key-based, i.e., t('home_string'), or literal like below
t('Home');
function t($text, $language = NULL)
{
if (!$language) {
// determine visitor's language
}
// look up translation in db/file
// if none found just return $text as is
}
I have some content types (nodes) that are attached to various taxonomies. For specific node types, I want to do some validation on the taxonomy. I do not want to hard-code the nodes types and their corresponding fields that reference the taxonomy. So I put them in array.
However, I am unable to dereference the field names. I've tried double $$, quotes, etc, but can't get it to work. Is what I want to do possible?
Below is a standalone PHP that I am trying to get to work.
<?php
$node = (object) array(
'nid' => NULL,
'vid' => NULL,
'uid' => '1',
'type' => 'price_document',
'language' => 'und',
'field_taxonomy_price' => array(
'und' => array(
array(
'tid' => '94'
)
)
),
);
$nodes_to_check = array("price_document" => "field_taxonomy_price",
"package" => "field_taxonomy_package",
);
if (array_key_exists($node->type,$nodes_to_check)) {
$taxonomy_field = $nodes_to_check[$node->type];
print_r($taxonomy_field);
$tid = $node->field_taxonomy_price ['und'][0]['tid']; // <- this works but, how
//$tid = $node->"$$taxonomy_field" ['und'][0]['tid']; <- can I deref variable?
}
?>
Well, you can do this:
$taxonomy_field = $nodes_to_check[$node->type];
$tid = $node->{$taxonomy_field}['und'][0]['tid];
You don't need the double dollar signs. That's in case you want to do things like this:
$dog = "I am a dog";
$var = "dog";
$$var = "Now I'm a pussycat";
echo $dog; // Output: Now I'm a pussycat
I have an array holding some default settings for my plugin. As the plugin evolves settings maybe removed or added from version to version.
Here is an example default array:
$defaults = array(
'setting1' => 'somevalue',
'setting2' => 'somevalue',
'setting4' => 'somevalue',
);
Here is an example of live settings data that the structure needs to be updated for the new $default structure:
$livesettings = array(
'setting1' => 'foo',
'setting2' => 'bar',
'setting3' => 'foobar',
);
I'm looking for a function where I can pass both arrays and the structure of the livesettings is updated to match the $defaults.
So in this case in livesettings:
setting1 and setting2 isn't touched. Their values stay intact
setting3 is removed as no longer needed
setting4 is added with the default value of somevalue
Are their any functions in PHP that can do this in one go? If yes what is it? If no how would I achieve this with PHP code?
You want a combination of array_intersect_key() and array_merge().
$livesettings = array_intersect_key($livesettings, $defaults);
$livesettings = array_merge($defaults, $livesettings);
The first function will remove all keys not found in $defaults, while the second would add items from $defaults not found in $livesettings
you don't need to a function for this problam, you can also use the $defaults like a base array,
$defaults = array(
'setting1' => 'somevalue',
'setting2' => 'somevalue',
'setting4' => 'somevalue',
);
$livesettings = $defaults; // it will be copited by value,
$livesettings['setting1'] = 'overriden setting 1';
$livesettings['setting3'] = 'added new setting to live config';