隐藏数组的空值

Posted

技术标签:

【中文标题】隐藏数组的空值【英文标题】:Hide the null value of an array 【发布时间】:2021-06-27 20:01:28 【问题描述】:

我有这个参数数组:

$fields = [
        'idutente' => $_POST['acf']['field_60084ad3970a8'] ?? 0,
        'nome' => $_POST['acf']['field_6050d30f14572'],
        'cognome' => $_POST['acf']['field_6050d32614573'],
        'titolo' => $_POST['acf']['field_605253f9f597d'],
        'sesso' => $_POST['acf']['field_605312ee27c34'],
        'ente' => $_POST['acf']['field_6048890606c82'],
        'ruolo' => $_POST['acf']['field_604888fc06c81'],
        'idnazione' => $_POST['acf']['field_60526f08d2fbb'],
        'citta' => $_POST['acf']['field_6048897606c87'],
        'provincia' => $_POST['acf']['field_60530b382fff6'],
        'indirizzo' => $_POST['acf']['field_6048894c06c85'],
        'telefono' => $_POST['acf']['field_6048899006c89'],
        'cellulare' => $_POST['acf']['field_604889a406c8b'],
        'email' => $_POST['acf']['field_6050d33414574'],
        'fax' => $_POST['acf']['field_6048899b06c8a'],
        'sitoweb' => $_POST['acf']['field_604889ad06c8c'],
        'password' => $_POST['acf']['field_60532814d263e'],
        'cap' => $_POST['acf']['field_6050f2f105bf9'],
        'socio' => $_POST['acf']['field_60526f6cad9a8'],
        'tipoente' => $_POST['acf']['field_6052701fe0b22'],
        'idcategoria' => join(',', 
            [
            join(',', $_POST['acf']['field_6051d1955e87e']),
            join(',', $_POST['acf']['field_605f869a3baff']),
            join(',', $_POST['acf']['field_605f86ac3bb00']),
            join(',', $_POST['acf']['field_6062f22f59a1b']),
            join(',', $_POST['acf']['field_6062f2a384747']),
            join(',', $_POST['acf']['field_6062f346e2421']),
            join(',', $_POST['acf']['field_6062f3e7e66d1']),
            join(',', $_POST['acf']['field_6062f4378975a']),
            join(',', $_POST['acf']['field_6062f4ceced5a']),
            ]
        ),
    ];

当我去检查我的答案时,除了“idcategoria”的值之外,一切正常。 如下所示:

,820,821,,,,,,,

当它找不到用于填充它的值时,如何防止它创建 ',' 为空?

【问题讨论】:

【参考方案1】:

您可以使用array_values(),如下所示

'idcategoria' => join(',', 
            array_values([
            join(',', $_POST['acf']['field_6051d1955e87e']),
            join(',', $_POST['acf']['field_605f869a3baff']),
            join(',', $_POST['acf']['field_605f86ac3bb00']),
            join(',', $_POST['acf']['field_6062f22f59a1b']),
            join(',', $_POST['acf']['field_6062f2a384747']),
            join(',', $_POST['acf']['field_6062f346e2421']),
            join(',', $_POST['acf']['field_6062f3e7e66d1']),
            join(',', $_POST['acf']['field_6062f4378975a']),
            join(',', $_POST['acf']['field_6062f4ceced5a']),
            ])
        ),

【讨论】:

不幸的是它一直向我返回空白值【参考方案2】:

感谢@dev_mustafa 的输入,我能够解决我的问题。

我使用了 array_filter,神奇的事情发生了。

'idcategoria' => join(',', 
    array_filter([
        join(',', $_POST['acf']['field_6051d1955e87e']),
        join(',', $_POST['acf']['field_605f869a3baff']),
        join(',', $_POST['acf']['field_605f86ac3bb00']),
        join(',', $_POST['acf']['field_6062f22f59a1b']),
        join(',', $_POST['acf']['field_6062f2a384747']),
        join(',', $_POST['acf']['field_6062f346e2421']),
        join(',', $_POST['acf']['field_6062f3e7e66d1']),
        join(',', $_POST['acf']['field_6062f4378975a']),
        join(',', $_POST['acf']['field_6062f4ceced5a']),
        ])
    ),

【讨论】:

以上是关于隐藏数组的空值的主要内容,如果未能解决你的问题,请参考以下文章

去除数组中的空值

如何序列化数组中的空值?

php怎么去除数组中的空值

PHP过滤数组中的空值

JOI:允许数组中的空值

PHP 删除数组中的空值