php - Replace key of array with the value of another array while looping through -
i have 2 multidimensional arrays. first 1 $properties
contains english names , values. second array contains translations. example
$properties[] = array(array("floor"=>"5qm")); $properties[] = array(array("height"=>"10m")); $translations[] = array(array("floor"=>"boden")); $translations[] = array(array("height"=>"höhe"));
(they multidimensional because contains more elements, shouldn't matter now)
now want translate array, @ end this:
$properties[] = array(array("boden"=>"5qm")); $properties[] = array(array("höhe"=>"10m"));
i have managed build foreach construct loop through these arrays, @ end not translated, problem is, how tell array replace key value.
what have done this:
//translate array foreach ($properties $propertyarray) { //need second foreach because multidimensional array foreach ($propertyarray $p_kivipropertynamekey => $p_propertyvalue) { foreach ($translations $translationarray) { //same above foreach ($translationarray $t_kivitranslationpropertykey => $t_kivitranslationvalue) { if ($p_kivipropertynamekey == $t_kivitranslationpropertykey) { //name found, save new array key $p_kivipropertynamekey = $t_kivitranslationvalue; } } } } }
the problem line save new key:
$p_kivipropertynamekey = $t_kivitranslationvalue;
i know part executed correctly , contains correct variables, believe false way assing new key.
this way should done:
$properties[$oldkey] = $translations[$newkey];
so tried one:
$propertyarray[$p_kivipropertynamekey] = $translationarray[$t_kivitranslationpropertykey];
as far understood, above line should change p_kivipropertynamekey of propertyarray value of translation array not receive error nor name translated. how should done correctly?
thank help!
additional info
this live example of properties array
array ( [0] => array ( [country_id] => 4402 ) [1] => array ( [iv_person_phone] => 03-11 ) [2] => array ( [companyperson_lastname] => kallio ) [3] => array ( [rc_lot_area_m2] => 2412.7 ) [56] => array ( [floors] => 3 ) [57] => array ( [total_area_m2] => 97.0 ) [58] => array ( [igglo_silentsale_realty_flag] => false ) [59] => array ( [possession_partition_flag] => false ) [60] => array ( [charges_parkingspace] => 10 ) [61] => array ( [0] => array ( [image_realtyimagetype_id] => yleiskuva ) [1] => array ( [image_itemimagetype_name] => kivirealty-original ) [2] => array ( [image_desc] => makuuhuone ) ) )
and live example of translations array
array ( [0] => array ( [addr_region_area_id] => maakunta [group] => kohde ) [1] => array ( [addr_town_area] => kunta [group] => kohde ) [2] => array ( [arable_no_flag] => ei peltoa [group] => kohde ) [3] => array ( [arableland] => pellon kuvaus [group] => kohde ) )
i can build translations array in way. did this, because in second step have check, group keys belong to...
according structure of $properties
, $translations
, somehow know how these connected. it's bit vague how indices of array match eachother, meaning values in $properties
@ index 0
equivalent translation in $translations
@ index 0
.
i'm wondering why $translations
array need have same structure (in nesting) $properties
array. opinion word height
can mean höhe
in german. representing array suggest there multiple translations possible.
so if narrow down $translations
array 1 dimensional array in:
$translation = array( "height"=>"höhe", "floor"=>"boden" );
a possible loop be
$result = array(); foreach($properties $i => $array2) { foreach($array2 $i2 => $array3) { foreach($array3 $key => $value) { $translatedkey = array_key_exists($key, $translations) ? $translations[$key]: $key; $result[$i][$i2][$translatedkey] = $value; } } }
(i see every body posting 2 loops, it's array,array,array
structure, not array,array
..)
if cannot narrow down translation array 1 dimensional array, i'm wondering if each index in $properties
array matches same index in $translations
array, if it's same trick adding indices (location):
$translatedkey = $translations[$i][$i2][$key];
i've used array_key_exists
because i'm not sure translation key present. have create logic each case scenario on check or not.
Comments
Post a Comment