1

I have an array $texts which looks like this:

Array
(
    [CAR_SINGULAR] => Array
        (
            [id] => 1
            [translations] => Array
                (
                    [de] => Auto
                    [en] => Car
                )
        )
    [CAR_PLURAL] => Array
        (
            [id] => 2
            [translations] => Array
                (
                    [de] => Autos
                    [en] => cars
                )
        )
)

and then I have this piece of PHP code where I want to output the array with a simple h1 and an input type text for each of the translation country codes.

foreach(array_keys($texts) as $text)
{ 
    echo "<h1>". $text ."</h1>";

    foreach($texts[$text] as $textData) 
    {  
        foreach(array_keys($textData) as $languageCode)
        {
            echo $languageCode .": <input type=\"text\" id=\"". $ID_OF_CODE_AND_TEXTID ."\" value=\"". $VALUE_OF_LANGUAGE_CODE ."\" /><br />";   
        }       
    } 
}

The result should be-

<h1>CAR_SINGULAR</h1>
de: <input type="text" id="de_1" value="Auto" /><br />
en: <input type="text" id="en_1" value="car" />

<h1>CAR_PLURAL</h1>
de: <input type="text" id="de_2" value="Autos" /><br />
en: <input type="text" id="en_2" value="cars" />

but somehow I am too blind to add the id and the value to the input :-(

Any help would be appreciated :-)

4
  • foreach(array_keys($textData) as $languageCode => $translation) Commented Jan 10, 2018 at 11:16
  • I am sorry but this will give me: 0: <input type="..." value="de" /> 1: <input type="..." value="en" /> Commented Jan 10, 2018 at 11:17
  • ah, it was correct. I had to adjust the output to: echo $translation .": <input type=\"text\" value=\"". $textData[$translation] ."\" /><br />"; Commented Jan 10, 2018 at 11:21
  • @rwur can you check my answer once Commented Jan 10, 2018 at 11:33

1 Answer 1

1

You have to add the html but this is the basic output:

$texts  = Array
(
    "CAR_SINGULAR" => Array
        (
            "id" => 1,
            "translations" => Array
                (
                    "de" => "Auto",
                    "en" => "Car"
                )
        ),
    "CAR_PLURAL" => Array
        (
            "id" => 2,
            "translations" => Array
                (
                    "de" => "Autos",
                    "en" => "cars"
                )
        )
);

foreach($texts as $key => $subarr){
    echo $key. "\n";
    foreach($subarr["translations"] as $lang => $val){
        echo $lang . ": " . $val . "\n";
    }
    echo "\n";
}

The Key of the first array is plural/singular.
Then I loop the inner subarray and output it's values in "translations".

https://3v4l.org/4AGmW

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

3 Comments

Ok, thanks but how to add the key in front of the input type and the id to the input? :-)
you mean language "de"/"en"? Added that to the code
echo $lang .": <input type=\"text\" id=\"". $lang . "_". $subarr['id'] ."\" value=\"". $val ."\" /><br />";

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.