Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 删除空JSON参数_Php_Json - Fatal编程技术网

Php 删除空JSON参数

Php 删除空JSON参数,php,json,Php,Json,我已经设置了一系列变量及其值: $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.e

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

$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

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);

在创建之前,请检查该值是否为空。不应手动更改json。您应该只添加以值开头的参数,或者在json编码之前遍历数组并删除所有空参数。您不需要将数组项创建为字符串,例如,
'amount'=>“$price\u amount”
-您只需编写
'amount'=>$price\u amount
。这还允许您在JSON中以实际数字而不是字符串的形式显示数字。在副本中,定义函数
array\u filter\u recursive()
的答案应该在后面执行。这应该在编码为JSON之前在数组上运行。另请参阅,这表明这通常比递归的
数组\u过滤器
更快。