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
It looks like you're getting the data in "pairs". Index 0 and 1 belong together, 2 and 3, and so on.
If that is true, you can split the data into chunks and process each pair:
$chunks = array_chunk($array, 2);
foreach($chunks as $chunk) {
// $chunk[0] contains object with title, qid, ...
// $chunk[1] contains "xml for question"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With