在第三个数组上使用 foreach 时出现致命错误
Fatal error when using foreach on 3rd array
尝试在第三个数组上使用 foreach 时出现错误
'{
"prefix": "_country-",
"countries": [
{
"code": "al",
"name": "Albania",
"cities": {
"prefix": "_city-",
"options": [
{
"code": "durres"
},
{
"code": "tirana"
}
]
}
},
{
"code": "dz",
"name": "Algeria",
"cities": {
"prefix": "_city-",
"options": [
{
"code": "algiers"
},
{
"code": "oran"
}
]
}
}
]
}
我的目标是获取上面的那些数据并期望它循环直到使用此代码的最后一个数据:
`foreach($countryarr1['countries'] as $countkey1 => $countname1){ ?> <br><br> <?php
foreach($countname1['cities'] as $cntrykey2 => $cntrys){
foreach($cntrys['options'] as $optionskey1 => $optionsarr1){
var_dump($optionsarr1);
}
}
}`
是returns这个错误
'Fatal error: Uncaught TypeError: Cannot access offset of type string on string in
C:\xampp\htdocs\homepage\countries.php:25 Stack trace: #0 {main} thrown in
C:\xampp\htdocs\homepage\countries.php on line 25'
这是第 25 行
Error on line 25
我错过了什么吗?顺便说一句,我正在为网站开发代理仪表板。
这里证明当我只有 2 个数组时没有问题:
foreach for 2 arrays
output with no errors
现在,当我在第 3 个数组上添加另一个 foreach 等等时,出现了错误。在这里。
foreach for 3rd array
错误error
像这样只用 2 个循环就可以很好地做到这一点
$countryarr1 = json_decode($str, true);
#print_r($countryarr1);
foreach($countryarr1['countries'] as $countkey1 => $countname1){ ?> <br><br> <?php
foreach($countname1['cities']['options'] as $optionskey1 => $optionsarr1){
var_dump($optionsarr1);
}
}
结果
array(1) { 'code' => string(6) "durres" }
array(1) { 'code' => string(6) "tirana" }
array(1) { 'code' => string(7) "algiers" }
array(1) {'code' => string(4) "oran" }
尝试在第三个数组上使用 foreach 时出现错误
'{
"prefix": "_country-",
"countries": [
{
"code": "al",
"name": "Albania",
"cities": {
"prefix": "_city-",
"options": [
{
"code": "durres"
},
{
"code": "tirana"
}
]
}
},
{
"code": "dz",
"name": "Algeria",
"cities": {
"prefix": "_city-",
"options": [
{
"code": "algiers"
},
{
"code": "oran"
}
]
}
}
]
}
我的目标是获取上面的那些数据并期望它循环直到使用此代码的最后一个数据:
`foreach($countryarr1['countries'] as $countkey1 => $countname1){ ?> <br><br> <?php
foreach($countname1['cities'] as $cntrykey2 => $cntrys){
foreach($cntrys['options'] as $optionskey1 => $optionsarr1){
var_dump($optionsarr1);
}
}
}`
是returns这个错误
'Fatal error: Uncaught TypeError: Cannot access offset of type string on string in
C:\xampp\htdocs\homepage\countries.php:25 Stack trace: #0 {main} thrown in
C:\xampp\htdocs\homepage\countries.php on line 25'
这是第 25 行
Error on line 25
我错过了什么吗?顺便说一句,我正在为网站开发代理仪表板。
这里证明当我只有 2 个数组时没有问题:
foreach for 2 arrays
output with no errors
现在,当我在第 3 个数组上添加另一个 foreach 等等时,出现了错误。在这里。
foreach for 3rd array
错误error
像这样只用 2 个循环就可以很好地做到这一点
$countryarr1 = json_decode($str, true);
#print_r($countryarr1);
foreach($countryarr1['countries'] as $countkey1 => $countname1){ ?> <br><br> <?php
foreach($countname1['cities']['options'] as $optionskey1 => $optionsarr1){
var_dump($optionsarr1);
}
}
结果
array(1) { 'code' => string(6) "durres" }
array(1) { 'code' => string(6) "tirana" }
array(1) { 'code' => string(7) "algiers" }
array(1) {'code' => string(4) "oran" }