【问题标题】:Arrays: filter based on inner arrays values [duplicate]数组:基于内部数组值的过滤器
【发布时间】:2016-11-03 01:43:03
【问题描述】:

过滤这个多维数组以仅获取带有['virtual'] == true的数组的最聪明的方法是什么??

array (size=3)
  0 => 
    array (size=13)
      'name' => string 'name' (length=4)
      'label' => string 'Nombre' (length=6)
      'sortable' => string 'true' (length=4)
      'property_to_show' => boolean false
      'image' => boolean false
      'virtual' => boolean false  <<<<<<<<<
      'filter_parameter' => boolean false
      'link' => boolean false
      'entity' => boolean false
      'boolean' => boolean false
      'route_parameters' => string 'id' (length=2)
      'text' => string '' (length=0)
      'route' => string '' (length=0)
  1 => 
    array (size=14)
      'virtual' => string 'true' (length=4)  <<<<<<<<<<<
      'label' => string 'Usuarios' (length=8)
      'link' => boolean true
      'class' => string 'DefaultBundle:User' (length=18)
      'entity' => string 'User' (length=4)
      'filter_parameter' => string 'company' (length=7)
      'text' => string 'ver' (length=3)
      'sortable' => boolean false
      'name' => string '' (length=0)
      'property_to_show' => boolean false
      'image' => boolean false
      'boolean' => boolean false
      'route_parameters' => string 'id' (length=2)
      'route' => string '' (length=0)
  2 => 
    array (size=14)
      'virtual' => string 'true' (length=4)  <<<<<<<<
      'label' => string 'Productos exclusivos' (length=20)
      'link' => boolean true
      'class' => string 'DefaultBundle:Product' (length=21)
      'entity' => string 'Product' (length=7)
      'filter_parameter' => string 'company' (length=7)
      'text' => string 'ver' (length=3)
      'sortable' => boolean false
      'name' => string '' (length=0)
      'property_to_show' => boolean false
      'image' => boolean false
      'boolean' => boolean false
      'route_parameters' => string 'id' (length=2)
      'route' => string '' (length=0)

【问题讨论】:

  • 应该如何对待'virtual' =&gt; string 'false'?还有,'virtual' =&gt; string '0.00' ... ?

标签: php


【解决方案1】:

您可以为此使用数组过滤器:

array_filter($array, function($value) {
   return !empty($value['virtual']);
});

【讨论】:

  • !empty($value['virtual']) 已经检查 $value['virtual'] 是否为真
  • 啊,你说得对,会编辑:)
猜你喜欢
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
相关资源
最近更新 更多