简体   繁体   English

在PHP中循环遍历数组/ Json对象

[英]Loop through Array/Json Objects in PHP

I have an array which consists of different nodes. 我有一个包含不同节点的数组。 following is the array when i print it using print_r($cruise). 以下是我使用print_r($ cruise)打印时的数组。

Array ( [SearchCruise_ApiInfoResponse] => Array ( [SearchCruise_ApiInfoResult] => {"TirunServer": [{"Sail_Date":"11/10/2020","Nights":"22","Brand_Code":"Z","ShipName":"AZAMARA JOURNEY","DepartText":"LISBON, PORTUGAL","Package_Id":"JR22U001","Package_Description":" 22-NIGHT WESTERN AFRICA JOURNEY","Ship_code":"JR","I":null,"o":"8530","b":"9780","d":null,"Mincost_iobd":"8530","Taxes_and_Fees_Amount":"326.60","Guest_1_2_Gratuity_Amount":"110.25","Ports":"Lisbon, portugal - Portimao, portugal - Casablanca, morocco - Agadir, morocco - Lanzarote, canary islands - Gran canaria, canary islands - At sea - At sea - Banjul, the gambia - At sea - At sea - Abidjan, ivory coast - Takoradi, ghana - At sea - At sea - At sea - Luanda, angola - At sea - At sea - Walvis bay, namibia - Luderitz, namibia - At sea - Cape town, south africa","SailingDates":",11/10/2020","Guest_1_Non_Commissionable_Cruise_Fare_Amount":"669.00","Fare_Code_Description":"STANDARD","Total":1}], "TotalCount": {"RecordCount":1}} ) )

What i want to do is access the Node "TirunServer" and loop through it to print all the values in a tabular format. 我想做的是访问节点“ TirunServer”并循环通过它以表格格式打印所有值。

Following is what i am doing. 以下是我在做什么。

foreach ($cruise as $key => $val){

foreach ($val as $key2 => $val2){

print_r($val2);

    for($i=0;$i<count($val2['TirunServer']);$i++){

        echo "okay = ".$val2['TirunServer'][$i]['Sail_Date'];
    }

}
}

i get the following while echoing ==> okay = { 当回显==> ok = {

Following is the value for print_r($val2) 以下是print_r($ val2)的值

{"TirunServer": [{"Sail_Date":"11/10/2020","Nights":"22","Brand_Code":"Z","ShipName":"AZAMARA JOURNEY","DepartText":"LISBON, PORTUGAL","Package_Id":"JR22U001","Package_Description":" 22-NIGHT WESTERN AFRICA JOURNEY","Ship_code":"JR","I":null,"o":"8530","b":"9780","d":null,"Mincost_iobd":"8530","Taxes_and_Fees_Amount":"326.60","Guest_1_2_Gratuity_Amount":"110.25","Ports":"Lisbon, portugal - Portimao, portugal - Casablanca, morocco - Agadir, morocco - Lanzarote, canary islands - Gran canaria, canary islands - At sea - At sea - Banjul, the gambia - At sea - At sea - Abidjan, ivory coast - Takoradi, ghana - At sea - At sea - At sea - Luanda, angola - At sea - At sea - Walvis bay, namibia - Luderitz, namibia - At sea - Cape town, south africa","SailingDates":",11/10/2020","Guest_1_Non_Commissionable_Cruise_Fare_Amount":"669.00","Fare_Code_Description":"STANDARD","Total":1}], "TotalCount": {"RecordCount":1}}

Any help 任何帮助

The $val2['TirunServer'] is array of objects, not array then you can not access like array. $ val2 ['TirunServer']是对象的数组,不是数组,则不能像数组那样访问。

Try this: 尝试这个:

echo "okay = ".$val2['TirunServer'][$i]->Sail_Date;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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