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

2
  • Is it not a duplicate of stackoverflow.com/questions/369602/… ? Do you really need to use Laravel ? Commented Nov 14, 2018 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 Commented Nov 14, 2018 at 18:46

4 Answers 4

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;
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @HCK for helping me day by day, working good, but it's not working for relationship keys
@user9500574 for that case you could use API Resources. I've just post an answer in another question, give it a read. Read the second approach that I mentioned.
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 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(...);
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

1 Comment

I tried to use except() before converting my collection to array, but I failed also, for that I converted it to a simple array
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');
}

Comments

-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']

5 Comments

laravel collection helper "only"
Please add some context, even a simple introduction such as "You can make use of Laravel Collection etc...". Also, the question is about nested arrays, so you're not fully answering the question here.
i.e $result = collect( $array )->map( function ($item) { return collect( $item)->only( [ 'id', 'name' ]); } )->toArray();
Actually missing a toArray() inside the closure but that actually replies to the question. Please edit your answer above accordingly.
You're right. The inner toArray() would be redundant.

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.