php - 使用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。
任何帮助将不胜感激。
解决方案
您可以使用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
}
}
}
推荐阅读
- django - UserCreationForm后django自动登录
- javascript - 图片下的 React,HTML,CSS 中心名称
- r - 方差比 - 过程
- node.js - 如何在使用节点调度的当天只运行一次任务
- jenkins - 詹金斯:PasswordParameterValue.value 需要 String 但收到 Secret
- rdf - 对象属性断言的 Protege 推理
- kubernetes - Kubernetes:如何在开始作业之前测试是否存在具有足够资源的节点
- r - 如何在 Rstudio 中转换日期格式
- javascript - JSX 没有相应的结束标记。ts(17014)
- python - 识别 pandas 数据框中的重叠事件(日期时间记录)