3

i'm looking for a way to remove empty values from a array.

If you see the code below, i'm trying to remove the empty values that get passed through before attaching it to the model. But this hasn't turned out this way so far.

Ofcourse i searched the web before asking, and so i know that trim() doesn't give the desired effect aswell as array_map() and the code below.

Looking for solutions, thanks!

if(Input::get('addcategorie'))
    {
        $new_cats = array();
        foreach(
            explode(
                ',', Input::get('addcategorie')) as $categorie) 
        {
            $categorie = Categorie::firstOrCreate(array('name' => $categorie));
            foreach($new_cats as $newcat)
            if($newcat == ' ' || $newcat == ' ' || $newcat == ''){
                unset($newcat);
            }
            array_push($new_cats, $categorie->id);
        }
        $workshop->categories()->attach($new_cats); 
    }
3
  • use the array_values method. Commented Jul 4, 2014 at 13:14
  • Can you explain @MattBurrow? Commented Jul 4, 2014 at 13:21
  • @NicolasHenrard you posted link to Javascript question Commented Jul 4, 2014 at 13:23

3 Answers 3

4

Just use array_filter:

$array = [
    0 => 'Patrick',
    1 => null,
    2 => 'Maciel',
    3 => '&nbsp',
    4 => ' '
];

$filtered = array_filter($array);

$nbsp = array_filter($array, function($element) {
    return $element == '&nbsp' OR  $element == ' ';
});

$clean = array_diff($filtered, $nbsp);

The return is:

array(2) {
  [0]=>
  string(7) "Patrick"
  [2]=>
  string(6) "Maciel"
}

This functions remove all null, empty and &nbsp from your arrays.

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

7 Comments

And what ' ' ? :)
Please note that also values which are an empty string or false get stripped out of the array. But I think it's the best solution for what the TS wanted to achieve.
Somehow it still adds the empty value as a new category. Could it be that the 'problem' is at 'firstOrCreate' ?
If i follow the code, it all makes sense and it should work, but somehow it doesn't. I'm quite certain the problem is at 'firstOrCreate'. I'll take a deeper look into that. Thanks!
@Jeroen maybe is your file encoding. Don't forget to mark the question as solved if is really that you said.
|
1

You should use the following code to unset in foreach:

foreach($new_cats as $key => $newcat) {
    if(yourCondition) {
        unset($new_cats[$key])
    }
}

1 Comment

Thanks for your answer, but this also does the same. Empty values are not removed.
0
foreach($new_cats as $newcat_i => $newcat) {
    $newcat = trim($newcat);

    if($newcat == ' ' || $newcat == '') {
        unset($new_cats[$newcat_i]);
    }
}

OR

$new_cats = array_filter(
    $new_cats, function ($value) {
        $value = trim($value);

        return $value != '' && $value != ' ';
    }
);

Final code is:

if(Input::get('addcategorie')) {
    $new_cats = array();
    foreach(explode(',', Input::get('addcategorie')) as $categorie) {
        $categorie = Categorie::firstOrCreate(array('name' => $categorie));

        array_push($new_cats, $categorie->id);
    }

    $new_cats = array_filter(
        $new_cats, function ($value) {
            $value = trim($value);

            return $value != '' && $value != ' ';
        }
    );

    $workshop->categories()->attach($new_cats);
}

1 Comment

tried both, still same result. Could it be that the 'problem' is at 'firstOrCreate' ?

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.