0

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 ;
    }
  });
}
3
  • There is a syntax error underneath the else if in your example. Commented Dec 21, 2018 at 15:29
  • @RossWilson Duly noted have edited the else if,, looking forward for assistance.. ~ Regards Commented Dec 21, 2018 at 15:36
  • is this what are you looking for $result = $rs['data']['unit_sales'] ?? $rs['data']['agents'];? Commented Dec 21, 2018 at 15:57

2 Answers 2

1

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

4 Comments

your logic is a bit much for just returning a variable, in the if-block you can just return $data->unit_sales;
,, Have tried your method but I get this error when I decode the response Trying to get property of non-object
can you try switching the notation, so instead of $rs->data, use $rs['data']
Yah it now works well,, In the if and else if blocks ,, how can I store them in a single variable and return the variable to the view..
0

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.

1 Comment

Duly noted,, Have edited the else if block,,am not sure about my logic in iterating the nested array

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.