首页 > 解决方案 > 使用php的对象数组中的空数组值

问题描述

我有一个像下面这样的数组。是否有任何方法或一行代码可以在没有 foreach 循环的情况下清空数组内的所有值。

$array=json_decode('{
  "client": "4",
  "gateWay": "1",
  "store": "store.shop.com",
  "valid": "true",
  "po": 34535,
  "additionalPO": 23423,
  "customerNotes": "",
  "orderItems": [
    {
      "item": "123",
      "quantity": 10,
      "supplierLotNo": "",
      "customsValue": "",
      "customsDescription": "",
      "hsCode": ""
    },
    {
      "item": "345",
      "quantity": 50
    }
  ],
  "shippingInfos": [
    {
      "address": {
        "city": "Chennai",
        "country": "India",
        "postalCode": "86715",
        "state": "TN",
        "streetAddress1": "6971 North Street",
        "streetAddress2": null
      },
      "contact": {
        "company": null,
        "email": "info@store.com",
        "firstName": "test",
        "lastName": "test",
        "phoneNo": null
      },
      "ServiceId": "3",
      "thirdPartyAccountNo": "",
      "signatureConfirmation": false,
      "saturdayDelivery": false
    }
  ]
}',true);

预期的输出应该是字符串值需要空字符串,整数值需要 0。

任何帮助将不胜感激。

标签: phparrays

解决方案


您可以使用array_walk,假设 boolean 设置为NULL,这里是代码。


代码

<?php

$array=json_decode('{
  "client": "4",
  "gateWay": "1",
  "store": "store.shop.com",
  "valid": "true",
  "po": 34535,
  "additionalPO": 23423,
  "customerNotes": "",
  "orderItems": [
    {
      "item": "123",
      "quantity": 10,
      "supplierLotNo": "",
      "customsValue": "",
      "customsDescription": "",
      "hsCode": ""
    },
    {
      "item": "345",
      "quantity": 50
    }
  ],
  "shippingInfos": [
    {
      "address": {
        "city": "Chennai",
        "country": "India",
        "postalCode": "86715",
        "state": "TN",
        "streetAddress1": "6971 North Street",
        "streetAddress2": null
      },
      "contact": {
        "company": null,
        "email": "info@store.com",
        "firstName": "test",
        "lastName": "test",
        "phoneNo": null
      },
      "ServiceId": "3",
      "thirdPartyAccountNo": "",
      "signatureConfirmation": false,
      "saturdayDelivery": false
    }
  ]
}',true);


($fx = function (&$in) use (&$fx) { is_array($in) ? array_walk($in, $fx) : ($in = is_string($in) ? "" : (is_int($in) ? 0 : NULL));})($array);

var_dump($array);

输出

array(9) {
  ["client"]=>
  string(0) ""
  ["gateWay"]=>
  string(0) ""
  ["store"]=>
  string(0) ""
  ["valid"]=>
  string(0) ""
  ["po"]=>
  int(0)
  ["additionalPO"]=>
  int(0)
  ["customerNotes"]=>
  string(0) ""
  ["orderItems"]=>
  array(2) {
    [0]=>
    array(6) {
      ["item"]=>
      string(0) ""
      ["quantity"]=>
      int(0)
      ["supplierLotNo"]=>
      string(0) ""
      ["customsValue"]=>
      string(0) ""
      ["customsDescription"]=>
      string(0) ""
      ["hsCode"]=>
      string(0) ""
    }
    [1]=>
    array(2) {
      ["item"]=>
      string(0) ""
      ["quantity"]=>
      int(0)
    }
  }
  ["shippingInfos"]=>
  array(1) {
    [0]=>
    array(6) {
      ["address"]=>
      array(6) {
        ["city"]=>
        string(0) ""
        ["country"]=>
        string(0) ""
        ["postalCode"]=>
        string(0) ""
        ["state"]=>
        string(0) ""
        ["streetAddress1"]=>
        string(0) ""
        ["streetAddress2"]=>
        NULL
      }
      ["contact"]=>
      array(5) {
        ["company"]=>
        NULL
        ["email"]=>
        string(0) ""
        ["firstName"]=>
        string(0) ""
        ["lastName"]=>
        string(0) ""
        ["phoneNo"]=>
        NULL
      }
      ["ServiceId"]=>
      string(0) ""
      ["thirdPartyAccountNo"]=>
      string(0) ""
      ["signatureConfirmation"]=>
      NULL
      ["saturdayDelivery"]=>
      NULL
    }
  }
}


推荐阅读