首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移除关联数组中的空值键-val对序列

移除关联数组中的空值键-val对序列
EN

Stack Overflow用户
提问于 2016-10-03 06:50:35
回答 1查看 46关注 0票数 0

我有这个数组

代码语言:javascript
复制
[1] => Array
    (
        [Assembly required] => Yes
        [Max load (kg)] => 120
        [Product weight (kg)] => 
        [Warranty (years)] => 3
        [Height adjustable] => Yes
        [Lumbar support] => 
        [Back tilt adjustment] => Yes
        [Seat tilt adjustment] => Yes
        [Chair height range (mm)] => 880 - 950
        [Chair seat width (mm)] => 500
        [Chair seat depth (mm)] => 480
        [Chair back height (mm)] => 410
        [Chair back width (mm)] => 420
        [Seat height range (mm)] => 440 - 580
        [AFRDI Approved] => 
        [Optional adjustable arms] => Yes
        [image] => https://cdn.shopify.com/s/files/1/1249/7859/files/Chair.png?18135080827462508830
    )

以上数组的var_dump是

代码语言:javascript
复制
         [1]=>
      array(17) {
        ["Assembly required"]=>
        string(3) "Yes"
        ["Max load (kg)"]=>
        string(3) "120"
        ["Product weight (kg)"]=>
        string(0) ""
        ["Warranty (years)"]=>
        string(1) "3"
        ["Height adjustable"]=>
        string(3) "Yes"
        ["Lumbar support"]=>
        string(0) ""
        ["Back tilt adjustment"]=>
        string(3) "Yes"
        ["Seat tilt adjustment"]=>
        string(3) "Yes"
        ["Chair height range (mm)"]=>
        string(9) "880 - 950"
        ["Chair seat width (mm)"]=>
        string(3) "500"
        ["Chair seat depth (mm)"]=>
        string(3) "480"
        ["Chair back height (mm)"]=>
        string(3) "410"
        ["Chair back width (mm)"]=>
        string(3) "420"
        ["Seat height range (mm)"]=>
        string(9) "440 - 580"
        ["AFRDI Approved"]=>
        string(0) ""
        ["Optional adjustable arms"]=>
        string(3) "Yes"
        ["image"]=>
        string(80) "https://cdn.shopify.com/s/files/1/1249/7859/files/Chair.png?18135080827462508830"
      }

我想从上面删除空白值对。

代码语言:javascript
复制
  foreach($singlearr as $key=>$value){      
         if(is_null($value) || $value == '')
             unset($singlearr[$key]);
    }

这删除了值为null 的键值对,但它按的顺序扭曲了序列。

代码语言:javascript
复制
    [1] => Array
    (
        [Assembly required] => Yes
        [Max load (kg)] => 120
        [Warranty (years)] => 3
        [Height adjustable] => Yes
        [Back tilt adjustment] => Yes
        [Chair height range (mm)] => 880 - 950
        [Chair seat width (mm)] => 500
        [Chair seat depth (mm)] => 480
        [Chair back height (mm)] => 410
        [Chair back width (mm)] => 420
        [Seat height range (mm)] => 440 - 580
        [image] => https://cdn.shopify.com/s/files/1/1249/7859/files/Chair.png?18135080827462508830
        [Seat tilt adjustment] => Yes
        [Optional adjustable arms] => Yes
    )

结果数组var_dump表单:

代码语言:javascript
复制
          [1]=>
      array(14) {
        ["Assembly required"]=>
        string(3) "Yes"
        ["Max load (kg)"]=>
        string(3) "120"
        ["Warranty (years)"]=>
        string(1) "3"
        ["Height adjustable"]=>
        string(3) "Yes"
        ["Back tilt adjustment"]=>
        string(3) "Yes"
        ["Chair height range (mm)"]=>
        string(9) "880 - 950"
        ["Chair seat width (mm)"]=>
        string(3) "500"
        ["Chair seat depth (mm)"]=>
        string(3) "480"
        ["Chair back height (mm)"]=>
        string(3) "410"
        ["Chair back width (mm)"]=>
        string(3) "420"
        ["Seat height range (mm)"]=>
        string(9) "440 - 580"
        ["image"]=>
        string(80) "https://cdn.shopify.com/s/files/1/1249/7859/files/Chair.png?18135080827462508830"
        ["Seat tilt adjustment"]=>
        string(3) "Yes"
        ["Optional adjustable arms"]=>
        string(3) "Yes"
      }

就像。在上面的结果数组中,[image]移动到上面,类似于这样。

EN

回答 1

Stack Overflow用户

发布于 2016-10-03 06:53:01

您可以使用

array_filter()

它将从数组中筛选出所有空值和空值。

正式文件

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39825924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档