Martin
Martin

Reputation: 557

Problem iterating through a multi-dimensional array in PHP Laravel from an API response

Working on a Laravel application whereby I am calling an API to fetch some data and display on the view. The data am getting back is a multi-dimensional array/nested array whereby the 1st array in the in the data object may come as unit_sales or agents as shown in the code below.

Now I am trying to create some PHP logic to check if the 1st array key is either unit_sales or agents fetch all the data under it and return as a variable. For instance if 1st array_key in data object below is unit_sales fetch all the data under it and store in a variable, if agents, fetch all the data under it, store in a variable and return.

NB~The API response is stored in a variable called rs

Response 1 from the API

{
    "request_time": "2018-12-21 16:56:22",
    "response_time": "2018-12-21 16:56:23",
    "status": "success",
    "message": "Hierachies",
    "data": {
        "unit_sales": [
            {
                "id": "11**",
                "agents": [
                    {
                        "agent_no": "68**",
                        "policies": [
                            "IL***********",
                            "IL************",
                            "IL***********"
                        ]
                    },
                    {
                        "agent_no": "53983",
                        "policies": [
                            "IL**********",
                            "IL***********",
                            "IL***********"
                        ]
                    }
                ]
            }
        ]
    }
}

Response 2 from the API

{
    "request_time": "2018-12-21 16:56:22",
    "response_time": "2018-12-21 16:56:23",
    "status": "success",
    "message": "Hierachies",
    "data": {
        "agents": [
            {
                "agent_no": "68**",
                "policies": [
                    "IL***********",
                    "IL************",
                    "IL***********"
                ]
            },
            {
                "agent_no": "53**",
                "policies": [
                    "IL**********",
                    "IL***********",
                    "IL***********"
                ]
            }
        ]
    }
}

Logic that I have written but got lost along the way

    if(array_key_exists("unit_sales",$rs)){
        $data= collect(collect($rs)->first()[0])['unit_sales'];
      $data = collect($data)->map(function($item){
        if(array_key_exists('unit_sales',$item)){
          $agents  = collect($item['unit_sales'])->map(function($item2){
            if(array_key_exists('agents',$item2)){
              return $item2['unit_sales'];
            }
        });
          return $unit;
        }else{
         return ;
        }
      });
    }

   else if(array_key_exists("agents",$rs)){
      $data= collect($rs)->'agents';
  $data = collect($data)->map(function($item){
    if(array_key_exists('agents',$item)){
      $agents  = collect($item['agents'])->map(function($item2){
        if(array_key_exists('agents',$item2)){
          return $item2['agents'];
        }
    });
      return $agents;
    }else{
     return ;
    }
  });
}

Upvotes: 0

Views: 244

Answers (2)

John Halsey
John Halsey

Reputation: 2008

looks like you just need to go into the data before you do the array_key_exists method?

maybe like this...

array_key_exists("unit_sales",$rs->data)

I also agree with @omphonia you will probably need to json_decode() the response first.

Upvotes: 0

comphonia
comphonia

Reputation: 521

If you are getting a json response, you need to decode it to access its keys, then values

$rs = json_decode($rs);

$data = $rs->data; //sets array to just the "data" key

if(array_key_exists("unit_sales",$data))
{
    //do stuff
} 
else if (array_key_exists("agents",$data))
{
    //do stuff
}

Upvotes: 1

Related Questions