質問

[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