doufei8250 2019-03-26 18:23
浏览 312
已采纳

如果满足条件,如何通过键从多维数组中删除数组Php

I have a array structured like so

array:2 [▼
  "id_1553623907416" => array:2 [▼
    "id_title" => "About"
    "id_content" => """
      <!DOCTYPE html>
      <html>
      <head>
      </head>
      <body>
      <p>Helllo world</p>
      </body>
      </html>
      """
  ]
  "id_1553623916174" => array:2 [▼
    "id_title" => "Education"
    "id_content" => """
      <!DOCTYPE html>
      <html>
      <head>
      </head>
      <body>
      <p>hello data</p>
      </body>
      </html>
      """
  ]
]

i need to be able to remove the array named id_1553623907416 if it contains the value About in the sub-array key id_title. The ids are dynamic so this has to be dynamically.

that array is stored in the variable @output.

 @foreach ($output as $item)    
   @if($item["id_title"] == "About")
      //remove array 
   @else
     //do something else 
   @endif
@endforeach
  • 写回答

1条回答 默认 最新

  • duanbei1709 2019-03-26 18:27
    关注

    Using your existing code (I don't know Laravel) just expose the key in the foreach and unset:

     @foreach ($output as $key => $item)    
       @if($item["id_title"] == "About")
          unset($output[$key]); 
       @endif
    @endforeach
    

    If there can be only one then add break; after the unset.

    Or you can filter them out:

    $output = array_filter($output, function($v) { return $v['id_title'] != 'About'; });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同