简体   繁体   中英

PHP - Set array key only if the value is not null

Is there a shorter solution for something like this?

$manufacturer = array();

if(!is_null($params->get('name'))){
    $manufacturer['name'] = $params->get('name');
}

if(!is_null($params->get('link'))){
    $manufacturer['link'] = $params->get('link');
}

if(!is_null($params->get('description'))){
    $manufacturer['description'] = $params->get('description');
}

...

So a key of an array should only be set with the value if the value is not null. This is a bit shorter, but with this solution the keys will exist with the value NULL. But they should not even exist when the value was NULL:

$manufacturer = array(
    'name' => !is_null($params->get('name')) ? $params->get('name') : null,
    'link' => !is_null($params->get('link')) ? $params->get('link') : null,
     'description' => !is_null($params->get('description')) ? $params->get('description') : null
);

EDIT:

It should work for multidimensional arrays and the array keys and param keys may vary

$keys = ['name', 'link', ....];
foreach ($keys as $key) {
    if(!is_null($params->get($key))){
        $manufacturer[$key] = $params->get($key);
    }
}

for @u_mulder foreach and @Nono array_filter solutions they work only for simple array, they do not remove null values from multidimensional arrays,

try this recursive function:

<?php
/**
 just keep your array like this:

$manufacturer = array(
    'name' => $params->get('name'),
    'link' => $params->get('link'),
    'description' => $params->get('description'),
    'attribute' => array (
        'street' => $params->get('street'),
        ...
    )
    ...
);
**/

$manufacturer = [
    'name' => 'yoeunes',
    'link' => null,
    'description' => 'fake description',
    'attribute' => [
        'street' => null,
        'city'   => 'Marrakech',
    ],
];

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

        if (is_null($array[$key])) {
            unset($array[$key]);
        }
    }

    return $array;
}

echo "<pre>";
print_r(array_remove_null($manufacturer));

output:

Array
(
    [name] => yoeunes
    [description] => fake description
    [attribute] => Array
        (
            [city] => Marrakech
        )

)

The foreach solutions are possible, but since the array key and params key may vary and because I have another array with values within this array, this is maybe a better solution, or what do you think?

$manufacturer = array(
    'name' => !is_null($params->get('name')) ? $params->get('name') : false,
    'link' => !is_null($params->get('link')) ? $params->get('link') : false,
    'description' => !is_null($params->get('description')) ? $params->get('description') : false,
    'attribute' => array (
        'street' => !is_null($params->get('street')) ? $params->get('street') : false,
        ...
    )
    ...
);
$manufacturer = array_filter($manufacturer);

array_filter (without callback) will remove all keys with false. (It's possible as long I don't have boolean values for my keys. If so you can do the same with NULL and then use a callback for array_filter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM