Ikhtiar Mahmud
Ikhtiar Mahmud

Reputation: 77

Get value of nested array from json

I have some JSON data which get from an API. I need only the products name from there. How can I get this? I am already trying with foreach loop but I can't. I am using laravel framework version 6.0

[
  {
    "id": 2,
    "name": "Bijj",
    "categories": [
                {
                  "id": 10,
                  "name": "Rice",
                  "sbu": 2,
                  "products" : [
                     {
                        "id": 25,
                        "name": "Rajkumar",
                        "skus": [],
                        "short_description": "বাংলাদেশের আবহাওয়া উপযোগী হাইব্রিড ধান",
                     },
                     {
                        "id": 50,
                        "name": "Sera",
                        "skus": [],
                        "short_description": "বাংলাদেশের আবহাওয়া উপযোগী হাইব্রিড ধান",
                     }

                  ]
               },
               {
                 "id": 20,
                 "name": "Vhutta",
                 "sbu": 2,
                  "products" : [
                     {
                         "id": 129,
                         "name": "Pack-139",
                         "skus": [],
                         "short_description": "মোচায় ৮৬ % দানা ও ১৪ % শাঁস",
                     },
                     {
                        "id": 125,
                        "name": "Don-12",
                        "skus": [],
                        "short_description": "সারা বৎসর চাষ উপযোগী",
                     }   
                  ]
                }
       ]
  }
]

I want to get only the all products name form there.

$getSbu = Helper::CreateGuzzleHttpClient('sbu');
foreach ($getSbu as  $value) {
     if($value->id == 2) {
          foreach ($value->categories as $category) {
                $products = $category->products;
             }
           }
        }

Upvotes: 0

Views: 113

Answers (4)

Achraf Khouadja
Achraf Khouadja

Reputation: 6279

Leverage aravel collections

// this is just ur data but as json string
$var = '[{"id":2,"name":"Bijj","categories":[{"id":10,"name":"Rice","sbu":2,"products":[{"id":25,"name":"Rajkumar","skus":[],"short_description":"বাংলাদেশের আবহাওয়া উপযোগী হাইব্রিড ধান"},{"id":50,"name":"Sera","skus":[],"short_description":"বাংলাদেশের আবহাওয়া উপযোগী হাইব্রিড ধান"}]},{"id":20,"name":"Vhutta","sbu":2,"products":[{"id":129,"name":"Pack-139","skus":[],"short_description":"মোচায় ৮৬ % দানা ও ১৪ % শাঁস"},{"id":125,"name":"Don-12","skus":[],"short_description":"সারা বৎসর চাষ উপযোগী"}]}]}]';

$objCollection = collect(json_decode($var, true));

$productNames = $objCollection->pluck('categories.*.products.*.name');

dd($productNames);

Upvotes: 0

baldev parmar
baldev parmar

Reputation: 21

After decode you can simply use this helper

use Illuminate\Support\Arr;

Arr::only($array, ['categories.products.name']);
or 
Arr::only($array, ['categories.*.name']);

Upvotes: 2

Hamelraj
Hamelraj

Reputation: 4826

you want get only product's name ??

and if your data array then you have to change $value['id']

$product_names = [];
foreach ($getSbu as  $value) {
     if($value->id == 2) {
          foreach ($value->categories as $category) {
                foreach ($category->products as $product) {
                     $product_names[] = $product->name;
                }
          }
      }
}
dd($product_names);

Upvotes: 1

pr1nc3
pr1nc3

Reputation: 8348

foreach (json_decode($getSbu) as  $value) {

Foreach works in an array so first you must decode it in order to parse it as an array.

Since you use object parsing you don't have to use parameter true in your json_decode function. If you want an array instead of an object you can use:

foreach (json_decode($getSbu,true) as  $value) {

but then you access your value like array fields, for example value['id']

Your code should look like:

$getSbu = Helper::CreateGuzzleHttpClient('sbu');
foreach (json_decode($getSbu) as  $value) {
     if($value->id == 2) {
          foreach ($value->categories as $category) {
                $products = $category->products;
             }
           }
        }

Upvotes: 0

Related Questions