5

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

1
  • 2
    foreach (['name', 'link', ...] as $key) if (!is_null($params->get($key))) ... perhaps…? Commented Aug 8, 2017 at 8:04

3 Answers 3

4

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
        )

)
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

Ok thats possible, but I thought there is maybe something else without foreach. Because the array key and params key may vary sometimes and on top of that I have another array with values within that array. It's still possible with foreach but I think not really much better.
This solution work only on simple array, it does not remove null values from multidimensional arrays
@Nano then you should edit your question to provide more details. The way it seems now, you need to deal with limited set of keys and simple values. If you need to handle multidimensional arrays that would be another scenario.
@Nima you right, @ Nono can you edit you're question please
0

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)

1 Comment

This solution work only on simple array, it does not remove null values from multidimensional arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.