문제

[responseheader] => Object (
    Array ( 
        [0] =>  Object ( 
            [id] => id_1 
            [name] => abc 
        )
        [1] => Object ( 
            [id] => id_2 
            [name] => xyz 
        ) 
    ) 
) 
[response] => Object ( 
    [id_1] => Object ( 
        [content] => Array ( 
            [0] => content_1 
            ) 
        ) 
    [id_2] => Object ( 
        [content] => Array ( 
            [0] => content_2
        )
    )
)

Both the above 2 objects, responseheader and response are under one object (header).

In the above structure, the order is for response will be same as it is in responseheader.(ie, id_2 will always come after id_1)

I want to get the content from response for each id present in responseheader. I will be iterating responseheader object.

I could loop through response and progressively add another property (say dummy) inside responseheader storing the content, but is there any better, faster approach?

도움이 되었습니까?

해결책

This should do the trick.

$result = array();
//loop through responseheader array
foreach($data['responseheader'] as $row)
{
    //if the id exists in the response array add it to the result array
    if(array_key_exists($row['id'], $data['response'])) {
        $result[] = $data['response'][$row['id']]['content'][0];
    }
}
print_r($result);

If the content in response could contain more than one content you have to loop throught it to:

foreach($data['response'][$row['id']]['content'] as $content) {
    $result[] = $content;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top