【问题标题】:Removing empty JSON parameters [duplicate]删除空 JSON 参数 [重复]
【发布时间】:2018-11-22 23:15:09
【问题描述】:

我已经设置了一个变量列表及其值:

$exp = "1642873358";
$country_code = "LO";
$item_description = "An item";
$language_code = "";
$price_amount = "5.99";
$price_currency = "EUR";
$urls_authorisation_callback = "https://www.example.com/";
$urls_redirect = "https://www.example.com/";
$urls_unsubscription_redirect = "";
$subscription_duration = "";
$subscription_unit = "";
$subscription_cycles = "";
$subscription_price_amount = "";
$subscription_price_currency = "";
$msisdn = "55555555";
$msisdn_edit_disabled = "";

这些变量值应该是可编辑的,如果愿意,可以将其留空,如示例中所示。我不想删除变量。

然后我创建了一个数组,该数组后来被编码为 JSON:

$token = array(
    'exp' => "$exp" ,
    'country_code' => "$country_code",
    'item_description' => "$item_description",
    'language_code' => "$language_code",
    'price' => array(
        'amount' => "$price_amount",
        'currency' => "$price_currency" 
    ),
    'urls' => array(
        'authorisation_callback' => "$urls_authorisation_callback",
        'redirect' => "$urls_redirect",
        'unsubscription_redirect' => "$urls_unsubscription_redirect"
    ),
    'subscription' => array(
        'duration' => "$subscription_duration",
        'unit' => "$subscription_unit" ,
        'cycles' => "$subscription_cycles" ,
        'price' => array(
            'amount' => "$subscription_price_amount",
            'currency' => "$subscription_price_currency"
        )
    ),
    'msisdn' => "$msisdn",
    'msisdn_edit_disabled' => "$msisdn_edit_disabled"
);

当我最终回显 JSON 文本时:

echo json_encode($token, JSON_PRETTY_PRINT) . "\n";

我得到以下信息:

{
    "exp": "1642873358",
    "country_code": "LO",
    "item_description": "An item",
    "language_code": "",
    "price": {
        "amount": "5.99",
        "currency": "EUR"
    },
    "urls": {
        "authorisation_callback": "https:\/\/www.example.com\/",
        "redirect": "https:\/\/www.example.com\/",
        "unsubscription_redirect": ""
    },
    "subscription": {
        "duration": "",
        "unit": "",
        "cycles": "",
        "price": {
            "amount": "",
            "currency": ""
        }
    },
    "msisdn": "55555555",
    "msisdn_edit_disabled": ""
}

如果我想删除未指定值的参数,我有哪些选择?以一种不会弄乱所需 JSON 结构的方式。给定示例中的期望结果是:

{
    "exp": "1642873358",
    "country_code": "LO",
    "item_description": "An item",
    "price": {
        "amount": "5.99",
        "currency": "EUR"
    },
    "urls": {
        "authorisation_callback": "https:\/\/www.example.com\/",
        "redirect": "https:\/\/www.example.com\/"
    },
    "msisdn": "55555555"
}

【问题讨论】:

  • 在创建之前检查该值不为空..
  • 您不应该手动更改 json。您应该只添加具有开头值的参数,或者在对数组进行 json 编码之前遍历数组并删除所有空参数。
  • 您不需要将数组条目创建为字符串,例如'amount' => "$price_amount" - 你可以写'amount' => $price_amount。这也可以让您将数字显示为 JSON 中的实际数字而不是字符串。
  • 在副本中,定义函数array_filter_recursive() 的答案应该满足您的需求。这应该在编码为 JSON 之前在数组上运行。

标签: php json


【解决方案1】:

将其转换为数组。使用 json_decode

然后使用下面的递归函数删除空值。然后再次编码为 json。

function array_remove_empty($haystack)
{
    foreach ($haystack as $key => $value) {
        if (is_array($value)) {
            $haystack[$key] = array_remove_empty($haystack[$key]);
        }

        if (empty($haystack[$key])) {
            unset($haystack[$key]);
        }
    }

    return $haystack;
}

$token = array_remove_empty($token);

【讨论】:

猜你喜欢
  • 2017-06-11
  • 2017-10-27
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
  • 2012-01-10
  • 2013-12-14
相关资源
最近更新 更多