dousu8456 2017-05-01 08:49
浏览 34
已采纳

如何在搜索字符串的基础上删除数组索引

Say I have a array like

    Array 
(
  [0] => Array ( [name] => test1 [type] => this1 [location] => 1 ) 
  [1] => Array ( [name] => test2 [type] => this2 [location] => 2 )
  [2] => Array ( [name] => testing2 [type] => this3 [location] => 3 )
)

and I want to remove the the key where anywhere it the array it contains the word "testing" doesn't have to be a exact match as long as it has that word.

so ideally [2] should be removed/unset because it contains that word. How can I achieve this.

Expected output:

    Array 
(
  [0] => Array ( [name] => test1 [type] => this1 [location] => 1 ) 
  [1] => Array ( [name] => test2 [type] => this2 [location] => 2 )
)

Thank you

  • 写回答

3条回答 默认 最新

  • doudaotui4297 2017-05-01 08:52
    关注

    Hope this will help you out.. Here we are using implode and stristr , we are joining an array into string using implode and searching string using stristr

    Try this code snippet here

    ini_set('display_errors', 1);
    
    $rows=Array ( 
        0 => Array ( "name" => "test1","type" => "this1", "location" => 1 ),
        1 => Array ( "name" => "test2" ,"type" => "this2", "location" => 2 ),
        2 =>Array ( "name" => "testing2","type" => "this3", "location" => 3 ));
    $wordToSearch="testing";
    foreach($rows as $key => $value)
    {
        if(stristr(implode("", $value), $wordToSearch))
        {
            unset($rows[$key]);
        }
    }
    print_r($rows);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)