Home > Enterprise >  how to remove array cell in laravel
how to remove array cell in laravel

Time:06-05

in laravel i have tow array like this

array one

array: [
  0 => "title1"
  1 => null
  2 => "title 3"
  3 => null
]

and this is my another array

array:4 [
  0 => "value 1"
  1 => null
  2 => null
  3 => "value 4"
]

i want to combine these tow array , so i used this method

array_combine($array1, $array2);

and this was the dd() result

array:3 [
  "title1" => "value 1"
  "" => "value 4"
  "title 3" => null
]

so far there is not any problem my question is how can i remove mereged array where key or value is null or empty

CodePudding user response:

You can leverage Laravel Collection

$merged = [
  "title1" => "value 1",
  "" => "value 4",
  "title 3" => null
];

$result = collect($merged)
    ->reject(fn($value, $key) => empty($value) || empty($key))
    ->all();

//$result will be
[
  "title1" => "value 1",
]
  • Related