我消費,返回稱爲$viewData
像這樣對象的數組Web服務:使用array_filter()返回兩個數組?
Array
(
[0] => stdClass Object
(
[id] => 64757
[title] => Frogger
[votes] => 1
[status] => gotit
)
[1] => stdClass Object
(
[id] => 64758
[title] => The Legend of Zelda
[votes] => 1
[status] => wantit
)
[2] => stdClass Object
(
[id] => 64759
[title] => Grand Theft Auto
[votes] => 1
[status] => wantit
)
)
我需要把這個分成兩個單獨的數組 - 包含狀態爲wantit所有的對象之一,而其他人的地位是。
我可以用array_filter()
用自定義函數獲取一個陣列出來的:
if(is_array($viewData) and (!empty($viewData))) {
function splitGames($v){
if ($v->status==="gotit") {
return true;
}
return false;
}
$gotEm = array_filter($viewData, "splitGames");
print_r($gotEm);
}
該函數返回我所期望的:
Array
(
[0] => stdClass Object
(
[id] => 64757
[title] => Frogger
[votes] => 1
[status] => gotit
)
)
是否有辦法來自動獲取原始數組剩下的部分放到第二個數組中,還是需要第二個自定義函數來查找「wantit」的狀態,然後在原始數組上重新運行array_filter()
?
array_filter只返回一個數組。你不能讓它在一次調用中返回多個數組。 – 2012-07-18 18:08:27