我的api调用提供了不正确的索引,如下所示
(
[0] => stdClass Object
(
[xml] =>
[qid] =>1
[title] => Tile of the question
[description] => Description of the question here
)
[1] => xml for quetion 1
[2] => stdClass Object
(
[xml] =>
[qid] => 2
[title] => Updated Question
[description] => description changed for edting
)
[3] => xml for quetion 2
)
我可以在foreach循环中访问值,但是问题是每个问题的xml都在循环的下一个索引中设置:
foreach ($array as $key =>$node) {
$title = $node->title;
$des = $node->description;
$qid = $node->qid;
if($node->xml==''){
// set xml value here in 1 and 3 index seen as in above output
}
}
我该怎么办
尝试一下:
foreach ($array as $key =>$node) {
try {
$title = $node->title;
$des = $node->description;
$qid = $node->qid;
if($node->xml==''){
$xml = $array[$key + 1];
}
echo "Added row with index $key";
} catch (\Throwable $th) {
echo "That was a xml row - The key is $key";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句