i have an improperly index being provided by an api call as below
(
[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
)
i can access the value in foreach loop but problem is that xml for each question is being set in next index on the loop:
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
}
}
how can i do that pls advise
Advertisement
Answer
just try this:
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";
}
}