0

I'm using Laravel, and I've a simple nested array

[
  ['id' => 1, 'name' => 'name1', 'phone' => '0'],
  ['id' => 2, 'name' => 'name2', 'phone' => '00'],
  ['id' => 3, 'name' => 'name3', 'phone' => '000']
]

I want to use Laravel array_forget() or any other simple way
to get this array without phones

user9500574
  • 142
  • 1
  • 11
  • Is it not a duplicate of https://stackoverflow.com/questions/369602/php-delete-an-element-from-an-array ? Do you really need to use Laravel ? – Gabriel Devillers Nov 14 '18 at 18:05
  • Thanks @GabrielDevillers, but my question was about nested array, I tried to use Laravel helpers, it helps me to shorten my code – user9500574 Nov 14 '18 at 18:46

4 Answers4

1

You could take a look at laravel's advanced collection methods - a handy bunch of helpers designed to work with array-like collections in Laravel. What you might need is namely the except() method

D. Petrov
  • 1,147
  • 15
  • 27
  • I tried to use except() before converting my collection to array, but I failed also, for that I converted it to a simple array – user9500574 Nov 14 '18 at 16:36
1

You could do:

$elements = [
  ['id' => 1, 'name' => 'name1', 'phone' => '0'],
  ['id' => 2, 'name' => 'name2', 'phone' => '00'],
  ['id' => 3, 'name' => 'name3', 'phone' => '000']
];

$elements_without_phones = collect($elements)->map(function ($element){
    array_forget($element, 'phone');

    return $element;
});
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
  • Thanks @HCK for helping me day by day, working good, but it's not working for relationship keys – user9500574 Nov 14 '18 at 16:42
  • @user9500574 for that case you could use [API Resources](https://laravel.com/docs/5.7/eloquent-resources#introduction). I've just post an answer in another question, give it a [read](https://stackoverflow.com/questions/53304346/laravel-change-every-model-in-collection/53304519#53304519). Read the second approach that I mentioned. – Kenny Horna Nov 14 '18 at 16:44
  • My array in fact was a converted from an eloquent collection, I tried your code but as I said, not working, for that, I converted it to an array, after that I converted it to array and using your code, not it's working great, CONFUSION :) – user9500574 Nov 14 '18 at 16:46
  • 1
    @user9500574 if your array came from an Eloquent collection, just avoid converting it to array and chain the `->map()` part in the eloquent collection: `$elements = Model::where('field', 'value')->get()->map(...);` – Kenny Horna Nov 14 '18 at 16:50
0

You have to iterate the array of arrays, and apply the array_forget() function on every (sub)array.

$array = [
    ['id' => 1, 'name' => 'name1', 'phone' => '0'],
    ['id' => 2, 'name' => 'name2', 'phone' => '00'],
    ['id' => 3, 'name' => 'name3', 'phone' => '000']
];

foreach($array as &$sub_array) { //note the passing by reference
    array_forget($sub_array, 'phone');
}
Dan D.
  • 815
  • 9
  • 16
-1
$collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);

$filtered = $collection->only(['product_id', 'name']);

$filtered->all();

// Output: ['product_id' => 1, 'name' => 'Desk']
Alessandro Benoit
  • 903
  • 10
  • 19