dongzhou1901 2015-04-22 23:32
浏览 60
已采纳

如果值部分匹配,则从多维数组中删除

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.

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?

  • 写回答

1条回答 默认 最新

  • duanjue6575 2015-04-22 23:45
    关注

    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]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效