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

如果满足条件,如何通过键从多维数组中删除数组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

图片转代码服务由CSDN问答提供 功能建议

我有一个像这样结构的数组

 数组:2  [▼
“id_15536​​23907416”=&gt; 数组:2 [▼
“id_title”=&gt;  “关于”
“id_content”=&gt;  msgstr“”“
&lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; p&gt; Helllo world&lt; / p&gt; 
  &lt; / body&gt; 
&lt; / html&gt; 
“”“
 
”
“id_15536​​23916174”=&gt; 数组:2 [▼
“id_title”=&gt;  “教育”
“id_content”=&gt;  msgstr“”“
&lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; p&gt; hello data&lt; / p&gt; 
  &lt; / body&gt; 
&lt; / html&gt; 
“”“
] 
] 
   
 
 

我需要能够删除名为的数组 id_15536​​23907416如果它包含子数组键id_title中的值About。 id是动态的,因此必须是动态的。

该数组存储在变量@output中。

  @foreach($ 输出为$ item)
 @if($ item [“id_title”] ==“关于”)
 //删除数组
 @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'; });
    
    点赞 打赏 评论

相关推荐 更多相似问题