2

I am working on some plugin in wordpress and I have following array and foreach loop with function in it.

Problem is that somehow I always getting $locale_key variable same as $code when $locale_key variable is inside function.

Please help.

$languages = array(
    array('af', 'af', 'Afrikaans'),
    array('ar', 'ar', 'العربية', 'rtl'),
    array('az', 'az', 'Azərbaycan'),
    array('be', 'bel', 'Беларуская мова'),
    array('bg', 'bg_BG', 'български'),
    array('bs', 'bs_BA', 'Bosanski'),
    array('ca', 'ca', 'Català'),
    array('cs', 'cs_CZ', 'Čeština'));

$lang = $_SESSION['lang'];

foreach ($languages as $key => $value) {
    $locale_key = $languages[$key][1];
    $code = $languages[$key][0];
    echo $locale_key; // Here i get for example "bs_BA"
    add_shortcode( $code, function($atts, $content = null, $locale_key) {
        global $lang;
        echo $locale_key; // And then here i get "bs"
        if ($lang == $locale_key) {
            return $content;
        }
    }); 
}
12
  • 1
    So what is your expected output? Commented Sep 2, 2015 at 11:17
  • my expected output is $languages[$key][1] from array, but i always get $languages[$key][0] Commented Sep 2, 2015 at 11:21
  • Can you please tell us what do u need as a output? Commented Sep 2, 2015 at 11:21
  • for example when in foreach loop $code is 'bs' i want to $locale_key be 'bs_BA', and that work if I echo variable outside function, but when i echo variable inside function i get same as $code variable. function($atts, $content = null, $locale_key) { global $lang; echo $locale_key; }); I will get bs, not bs_BA what I need. Commented Sep 2, 2015 at 11:30
  • Is add_shortcode() your created function? Commented Sep 2, 2015 at 11:43

2 Answers 2

1

Try:

$languages = array(
    array('af', 'af', 'Afrikaans'),
    array('ar', 'ar', 'العربية', 'rtl'),
    array('az', 'az', 'Azərbaycan'),
    array('be', 'bel', 'Беларуская мова'),
    array('bg', 'bg_BG', 'български'),
    array('bs', 'bs_BA', 'Bosanski'),
    array('ca', 'ca', 'Català'),
    array('cs', 'cs_CZ', 'Čeština'));

$lang = $_SESSION['lang'];

foreach ($languages as $key => $value) {
    $locale_key = $value[1];
    $code       = $value[0];
    add_shortcode( $code, function($atts, $content = null, $locale_key) {
        global $lang;
        if ($lang == $locale_key) {
            return $content;
        }
    }); 
}

When you use foreach($array as $key => $value) you can access the index via $key and the corresponding value via $value (even if that is an array too).

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

3 Comments

foreach ($languages as $key => $value) { $locale_key = $value][1]; $code = $value[0]; echo $locale_key; // HERE I GET "bs_BA" add_shortcode( $code, function($atts, $content = null, $locale_key) { global $lang; echo $locale_key; // HERE I GET "bs" if ($lang == $locale_key) { return $content; } }); }
@Antonio I think there is something wrong in your hook/callback function. But sadly I'm not using Wordpress so I cannot give you a solution. The only thing I can recommend you is to read the documentation but I think you already did that.
here is calback function: function add_shortcode($tag, $func) { global $shortcode_tags; $shortcode_tags[ $tag ] = $func; }
0
<?php
$array = [
    [1, 2, 3],
    [3, 4, 5],
];

foreach ($array as list($a, $b, $c)) {
    // $a enthält das erste Element des verschachtelten Arrays
    // und $b enthält das zweite Element
    echo "A: $a; B: $b; C: $c\n";
}
?>

Try something like above, im sure you will find the solution.

1 Comment

This code give me same result as my. I think that problem is somewhere in function, not in loop.

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.