-4

I have multiple array having the last index with 0 value and needs to remove that specific index. how can I do that in PHP? Thanks

array that index 3 has 0 value

2 Answers2

2

If you are looking for a Laravel way of doing it, you could achieve this with Collections.

$data = [
    'edu_country' => ['1', '11', '0'], 
    'edu_grade' => ['1', '1', '0']
]; 

return collect($data)->map(function($value) {
    return collect($value)->reject(function($value, $key) {
        return $value === '0'; 
    })->toArray();
})->toArray();

There might be an easier way of doing this with a Collection as well. I need to research a bit more.

Mozammil
  • 8,520
  • 15
  • 29
1

You can use array_filter. Reference : php.net/manual/en/function.array-filter.php

// Select only non-zero value indices
$newArray = array_filter($originalArray, function ($a) {
            return $a != 0;
            });
Sreejith BS
  • 1,183
  • 1
  • 9
  • 18