1

I'm trying to fetch data coming from a request, I used PostMan to send the request. the following arrays are the output of dd($items); ... The data that I want from these arrays are the id and quantity

array:2 [ 0 => array:12 [ "id" => 4 "name" => "omnis" "image_name" => "https://source.unsplash.com/random" "description" => "Officiis ea provident eius perspiciatis provident et eveniet illo. Tempore ullam ab molestiae unde. Fugit pariatur consequatur sunt veritatis. Laudantium quos veritatis quasi dolorum." "price" => 9 "discount" => 0 "status" => 1 "category_id" => 24 "place_id" => 1 "created_at" => "2019-12-15 08:05:36" "updated_at" => "2019-12-15 08:05:36" "quantity" => 5 ] 1 => array:12 [ "id" => 5 "name" => "molestiae" "image_name" => "https://source.unsplash.com/random" "description" => "Debitis dignissimos est veritatis veritatis sit. Ut ex non nam aliquid dolore vero earum. Ab aliquid et quibusdam enim." "price" => 7 "discount" => 0 "status" => 1 "category_id" => 25 "place_id" => 1 "created_at" => "2019-12-15 08:05:36" "updated_at" => "2019-12-15 08:05:36" "quantity" => 3 ] ] 

I want to do foreach loop to get this:

{ "id":4, "quantity":5 }, { "id":5, "quantity":3 } 

It tried :

 $item_array = []; foreach ($items as $key=>$item) { $item_array[$key] = $item['id']; $item_array[$key] = $item['quantity']; } 

4 Answers 4

1

The data that I want from these arrays are the id and quantity

that is:

 $item_array = []; foreach ($items as $item) { $item_array[] = [ 'id' => $item['id'], 'quantity' => $item['quantity'] ]; } 

or if you'd like to use true Laravel way:

use Illuminate\Support\Arr; $result = array_map( function($x) { return Arr::only($x, ['id', 'quantity']); }, $source ); 

or collect($source)->map(...) to utilize collection instead of array.

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

1 Comment

Yep. :) Updated again. Frankly speaking, the first version was okay but not clear what the source is. Now it should work.
1

Change your loop into

foreach ($items as $key=>$item) { $item_array[$key] = array( 'id' => $item['id'], 'quantity' => $item['quantity'] ); } 

Comments

1

you should do it like this:

 $item_array = []; foreach ($items as $key=>$item) { $obj = (object) ["id"=>$item['id'],"quantity"=>$item['quantity']]; $item_array[$key]=$obj; } 

Comments

1
use Illuminate\Support\Arr; collect($items)->map(function($item) { return Arr::only($item, ['id', 'quantity']); })->all(); 

5 Comments

It should work with a note: the result is a collection, and NOT an array. It can or con not make sense depending on context. As for response()->json($result) it should work like a charm :)
@artoodetoo the all method call is what returns it as an array, so it isn't a collection
The whole set is a collection of arrays. I meant it. But again, it is probably not a problem.
@artoodetoo it is array. see:laravel.com/docs/5.2/collections#method-all
@TsaiKoga you're right. I was inattentive. all() method turns a collection back into an array. 👍

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.