Home > OS >  multi array fetch language with $_GET['en'] PHP
multi array fetch language with $_GET['en'] PHP

Time:10-04

i have little complex array with languag english and french, i just want switch with button $_GET['lang-fr'] and $_GET['lang-en']

that my array and code with foreach...

i have array multidmension, with first key, lang-fr => and i use 3 foreach but i don't know if i have best practice


$experiences = [
    'lang-fr' => [
        [
            'name' => 'poissonerie',
            'address' => 'impasse de la tête coupee',
            'date' => '1350',
            'description' => 'bonne experience, patron delicieux avec ses employes',
        ],
    ],

    'lang-en' => [
        [
            'name' => 'blablz',
            'address' => 'cut head',
            'date' => '1350',
            'description' => 'boss so delicious',
        ],
    ],
];




foreach ($experiences as $lang => $experience) {
    if (isset($_GET['lang-fr'])) {
        foreach ($experience as $arrayIn => $other) {
            echo ' <div >';
            echo ' <span ></span>';

            echo '     <span ></span>';
            echo '</div>';
            echo '<div >';

            foreach ($other as $key => $value) {
                switch ($key) {
                    case 'address' === $key:
                        echo '<i>'.$lang[$value].'</i>'.'</br>';

                        break;

                    case 'date' === $key:
                        echo '<time>'.$lang[$value].'</time>'.'</br>';

                        break;

                    case 'description' === $key:
                        echo '<mark>'.$lang[$value].'</mark>'.'</br>';

                        break;

                    default:'name' === $key;
                        echo '<h4>'.$lang[$value].'<h4>';

                        break;
                }
            }
            echo '</br></div>';
        }
    }

    

CodePudding user response:

You don't need the first foreach. Just use the element of $experience corresponding to which of the $_GET['lang-XX'] is set.

if (isset($_GET['lang-fr'])) {
    $experience = $experiences['lang-fr'];
} elseif (isset($_GET['lang-en'])) {
    $experence = $experiences['lang-en'];
} else {
    die("Language not set"); // or you could set a default here
}

foreach ($experience as $arrayIn => $other) {
    echo ' <div >';
    echo ' <span ></span>';

    echo '     <span ></span>';
    echo '</div>';
    echo '<div >';

    foreach ($other as $key => $value) {
        switch ($key) {
            case 'address' === $key:
                echo '<i>'.$lang[$value].'</i>'.'</br>';

                break;

            case 'date' === $key:
                echo '<time>'.$lang[$value].'</time>'.'</br>';

                break;

            case 'description' === $key:
                echo '<mark>'.$lang[$value].'</mark>'.'</br>';

                break;

            default:'name' === $key;
                echo '<h4>'.$lang[$value].'<h4>';

                break;
        }
    }
    echo '</br></div>';
}

CodePudding user response:

your are the best, best regard friend:)

  • Related