简体   繁体   中英

Foreach with a multidimensional array - Laravel Blade

I am working on an air ticket reservation system project at laravel 8. When I call API I get a multidimensional array in return. Here is the array. I need to show 'ResultID', 'BaseFare','Tax' under the 'Fares' array

array:3 [▼
"SearchId" => "fca84241-dd9b-4cf2-bd6f-2f221dcc2b91"
"Results" => array:8 [▼
0 => array:14 [▼
  "ResultID" => "f0a54871-d728-424a-8e97-94e22308ac44"
  "IsRefundable" => true
  "Fares" => array:1 [▼
    0 => array:9 [▼
      "BaseFare" => 2775.0
      "Tax" => 725.0
      "Currency" => "BDT"
      "OtherCharges" => 0.0
      "Discount" => 0.0
      "AgentMarkUp" => 0.0
      "PaxType" => "Adult"
      "PassengerCount" => 1
      "ServiceFee" => 0.0
    ]
  ]
  "Discount" => 0.0
  "Validatingcarrier" => "BG"
  "LastTicketDate" => "2022-05-30T00:00:00"
  "segments" => array:1 [▶]
  "TotalFare" => 3500.0
  "TotalFareWithAgentMarkup" => 3500.0
  "Currency" => "BDT"
  "Availabilty" => 1
  "FareType" => "InstantTicketing"
  "isMiniRulesAvailable" => false
  "HoldAllowed" => false
]
1 => array:14 [▶]
2 => array:14 [▶]
3 => array:14 [▶]
4 => array:14 [▶]
5 => array:14 [▶]
6 => array:14 [▶]
7 => array:14 [▶]
]
"Error" => null
]

This is my controller function if it is useful in any case.

       $response = $client->post($url,
               ['form_params' => $travel_data]
       );

       $response = json_decode($response->getBody(), true);

       $data['flight']= $response;
       $data['app']= App_setting::find(1);
       return view('agent.flights.search_results',$data);

Below is my code block in blade.php but it gives me the error, I tried several methods but was failed to create the loop.

@foreach ($flight as $key => $teamArray)
         @foreach($teamArray as $team_Array)
               @php
                      echo "<pre>";
                      print_r($teamArray['Fares'][0]['BaseFare']);
               @endphp
                   
         @endforeach

   @endforeach

You are using the variable teamArray instead of team_Array in the inner foreach, but you can omit the second foreach.

To get the value of * SearchId* and the values of BaseFare and Tax :

$flight = [
    [
        "SearchId" => "fca84241-dd9b-4cf2-bd6f-2f221dcc2b91",
        "Results" => [
            0 => [
                "ResultID" => "f0a54871-d728-424a-8e97-94e22308ac44",
                "IsRefundable" => true,
                "Fares" => [
                    0 => [
                        "BaseFare" => 2775.0,
                        "Tax" => 725.0,
                        "Currency" => "BDT"
                    ]
                ],
                "Discount" => 0.0,
                "Validatingcarrier" => "BG"
            ]
        ]
    ]
];

foreach ($flight as $key => $teamArray) {
    echo $teamArray["SearchId"] . PHP_EOL;
    foreach($teamArray["Results"] as $result) {
        echo ($result["Fares"][0]["BaseFare"]) . PHP_EOL;
        echo ($result["Fares"][0]["Tax"]) . PHP_EOL;
    }
}

Output

fca84241-dd9b-4cf2-bd6f-2f221dcc2b91
2775
725

See a PHP demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM