2

I have two multidimensional arrays. First one $properties contains english names and their values. My second array contains the translations. An example

$properties[] = array(array("Floor"=>"5qm"));
$properties[] = array(array("Height"=>"10m"));

$translations[] = array(array("Floor"=>"Boden"));
$translations[] = array(array("Height"=>"Höhe"));

(They are multidimensional because the contains more elements, but they shouldn't matter now)

Now I want to translate this Array, so that I its at the end like this:

$properties[] = array(array("Boden"=>"5qm"));
$properties[] = array(array("Höhe"=>"10m"));

I have managed to build the foreach construct to loop through these arrays, but at the end it is not translated, the problem is, how I tell the array to replace the key with the value.

What I have done is this:

//Translate Array
foreach ($properties as $PropertyArray) {
    //need second foreach because multidimensional array
    foreach ($PropertyArray as $P_KiviPropertyNameKey => $P_PropertyValue) {
        foreach ($translations as $TranslationArray) {
            //same as above
            foreach ($TranslationArray as $T_KiviTranslationPropertyKey => $T_KiviTranslationValue) {
                if ($P_KiviPropertyNameKey == $T_KiviTranslationPropertyKey) {
                    //Name found, save new array key
                    $P_KiviPropertyNameKey = $T_KiviTranslationValue;
                }
            }
        }
    }
}

The problem is with the line where to save the new key:

$P_KiviPropertyNameKey = $T_KiviTranslationValue;

I know this part is executed correctly and contains the correct variables, but I believe this is the false way to assing the new key.

This is the way it should be done:

$properties[$oldkey] = $translations[$newkey];

So I tried this one:

$PropertyArray[$P_KiviPropertyNameKey] = $TranslationArray[$T_KiviTranslationPropertyKey];

As far as I understood, the above line should change the P_KiviPropertyNameKey of the PropertyArray into the value of Translation Array but I do not receive any error nor is the name translated. How should this be done correctly?

Thank you for any help!

Additional info

This is a live example of the 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 this is a live example of the 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 the translations array in another way. I did this like this, because in the second step I have to check, which group the keys belong to...

8
  • Thx! I want to replace the key of $properties with the value of $translations Commented Apr 18, 2013 at 12:18
  • Is the array always one child deep ? Commented Apr 18, 2013 at 12:19
  • Noo unfortunatedly not... I thought first get the first childs translated, then worry about the second lvl... Commented Apr 18, 2013 at 12:20
  • @Kaktus, just out of interest, could you display one example of how the structure is of a complete array assigned to $properties[]? Because an array,array,array somehow seems ambiguous to me .. Second, how come the structure of the $translations array is just like the $properties array? Wouldn't it be easier to have a translation array that's based on the key? Commented Apr 18, 2013 at 12:22
  • I added an live example... The key names do not match in the example because I took only a little part, but they match in the real array ... What did you mean with Wouldn't it be easier to have a translation array that's based on the key? I'd love to hear anything which makes this easier!! Commented Apr 18, 2013 at 12:34

4 Answers 4

2

Try this :

$properties     = array();
$translations   = array();

$properties[]   = array("Floor"=>"5qm");
$properties[]   = array("Height"=>"10m");

$translations[] = array("Floor"=>"Boden");
$translations[] = array("Height"=>"Höhe");

$temp           = call_user_func_array('array_merge_recursive', $translations);

$result         = array();
foreach($properties as $key=>$val){
   foreach($val as $k=>$v){
       $result[$key][$temp[$k]] = $v;
   }  
}

echo "<pre>";
print_r($result);

output:

Array
(
    [0] => Array
        (
            [Boden] => 5qm
        )

    [1] => Array
        (
            [Höhe] => 10m
        )

)

Please note : I changed the array to $properties[] = array("Floor"=>"5qm");, Removed a level of array, I guess this is how you need to structure your array.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you it works fine in your example, but unfortunatedly the arrays are multidimensional.. I could probably divert them, make of the group a third array, but its important that its the most effective way to handle this because its about a huge xml file. How can I translate your example into mine?
The TS has more then one child.
In my answer arays are alredy two dimensional...Why exactly you need it to be multi ?
Wow your right, I works! I am so thankful! But I receive some errors, I believe there are few line which do not have any translation. Two times Notice: Undefined index: (I believe these are the two with missing translations) and a Warning: Illegal offset type .. This one I do not understand. (All errors refer to the $result[$key][$temp[$k]] = $v; line.)
hmmmm...if you dont have translation it will show error, We can remove this error by checking array_key_exists, but then the corresponding value will not be displayed, it is better to add translation value.
|
0

According to the structure of $properties and $translations, you somehow know how these are connected. It's a bit vague how the indices of the array match eachother, meaning the values in $properties at index 0 is the equivalent for the translation in $translations at index 0.

I'm just wondering why the $translations array need to have the same structure (in nesting) as the $properties array. To my opinion the word Height can only mean Höhe in German. Representing it as an array would suggest there are multiple translations possible.

So if you could narrow down the $translations array to an one dimensional array as in:

$translation = array(
  "Height"=>"Höhe",
  "Floor"=>"Boden"
);

A possible loop would be

$result = array();

foreach($properties as $i => $array2) {
  foreach($array2 as $i2 => $array3) {
    foreach($array3 as $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 an array,array,array structure, not array,array ..)

If you cannot narrow down the translation array to a one dimensional array, then I'm just wondering if each index in the $properties array matches the same index in the $translations array, if so it's the same trick by adding the indices (location):

$translatedKey = $translations[$i][$i2][$key];

I've used array_key_exists because I'm not sure a translation key is always present. You have to create the logic for each case scenario yourself on what to check or not.

1 Comment

Thank you all so much! I believe I really made it too complicated. As you suggested, its better to make 3 one dimensional arrays, first translate the first array, then compare it to the groups (the groups were not mentioned in the example)
0

This is a fully recursive way to do it.

 /* input */
$properties[] = array(array("Floor"=>"5qm", array("Test"=>"123")));
$properties[] = array(array("Height"=>"10m"));

$translations[] = array(array("Floor"=>"Boden", array("Test"=>"Foo")));
$translations[] = array(array("Height"=>"Höhe"));


function array_flip_recursive($arr) {
    foreach ($arr as $key => $val) {
        if (is_array($val)) {
            $arr[$key] = array_flip_recursive($val);
        }
        else {
           $arr = @array_flip($arr);
       }
    }
    return $arr;
}

function array_merge_it($arr) {
    foreach ($arr as $key => $val) {
        if (is_array($val)) {
            $arr[$key] = array_merge_it($val);
        } else {
            if(isset($arr[$key]) && !empty($arr[$key])) {
              @$arr[$key] = $arr[$val];
            }
        }
     }
    return $arr;
}

function array_delete_empty($arr) {
    foreach ($arr as $key => $val) {
        if (is_array($val)) {
            $arr[$key] = array_delete_empty($val);
        }
        else {
           if(empty($arr[$key])) {
             unset($arr[$key]);
           }
       }
    }
    return $arr;
}



$arr = array_replace_recursive($properties, $translations);
$arr = array_flip_recursive($arr);
$arr = array_replace_recursive($arr, $properties);
$arr = array_merge_it($arr);
$arr = array_delete_empty($arr);

print_r($arr);

http://sandbox.onlinephpfunctions.com/code/d2f92605b609b9739964ece9a4d8f389be4a7b81

Comments

-1

You have to do the for loop in this way. If i understood you right (i.e) in associative array first key is same (some index).

foreach($properties as $key => $values) {
    foreach($values as $key1 => $value1) {
        $propertyResult[] = array($translations[$key][$key1][$value1] => $properties[$key][$key1][$value1]);
    }
}

print_r($propertyResult);

3 Comments

$values will give you array(array("Floor"=>"5qm")), $values1 will give you array("Floor"=>"5qm"), using $value1 as in $translations[$key][$key1][$value1] will throw a warning Illegal offset type. If you post a comment to the OP saying do some response or else dont post the questions, then please do post some decent valid code the next time.
@dbf: Dont you have eyes. See the code properly. It is not $values1, it is $value1. Open your eyes lyk f--king and see it correctly..
I would would hate this world too, being you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.