Remove from multidimensional array if value partially matches

I write a multilingual wordpress homepage. I need to load all languages into array, then detect the user language and then depending on user language remove the other languages from the array.

I want to do partial check on array to see if “Title” contains _DE or _ES and then depending on result remove one or the other from array.

Read More

So far I have

Data

array(2) { 
[1]=> array(3) { ["order"]=> string(1) "1" ["title"]=> string(9) "Slider_ES" ["id"]=> string(3) "500" } 

[2]=> array(3) { ["order"]=> string(1) "2" ["title"]=> string(11) "Slider_DE" ["id"]=> string(3) "493" } 

} 

Logic

$current_language = get_locale(); // WordPress function which returns either es_ES or de_DE codes

if ($current_language == es-ES) {

    foreach ($array as $key => $item) {
        if ($item['title'] === '_DE') {
            unset($array[$key]);
        }
    }

} else {

    foreach ($array as $key => $item) {
        if ($item['title'] === '_ES') {
            unset($array[$key]);
        }
    }

}

What did I miss?

Related posts

Leave a Reply

1 comment

  1. You can use the strpos function which is used to find the occurrence of one string inside other like this:

    if (strpos($item['title'],'_DE') !== false) {
        unset($array[$key]);
    }