這是一個非常愚蠢的問題,我不敢相信我會問這樣簡單的事情。從對象數組中獲取對象數組的字段值
我使用db->get['table']->result()
從表中獲取數據。
表模式如下所示:table(id,col1,col2)。
db->get['table']->result()
回報像這樣(的print_r):
Array
(
[0] => stdClass Object
(
[id] => 1
[col1] => "id 1 col 1"
[col2] => "id 1 col 2"
)
[1] => stdClass Object
(
[id] => 2
[col1] => "id 2 col 1"
[col2] => "id 2 col 2"
)
[2] => stdClass Object
(
[id] => 3
[col1] => "id 3 col 1"
[col2] => "id 3 col 2"
)
}
現在我需要從行COL2值具有ID = 2,我想這樣做沒有 「foreach」 循環。
我想我能做到這一點是這樣的:
$valueThatINeed = $myArray[2]->col2;
這是錯誤的,我知道爲什麼它的錯。
問題是 - 如何直接得到我所需要的沒有循環?
1.您能否更容易理解? 2.我的ID是唯一的,沒有辦法得到多個結果。 3.這是更有效的循環搜索ID? – Kamil
它看起來像在PHP 5中,我不能寫這樣的東西:'函數($ arrayValue)使用($ ID)',或者我不能把函數體放在那裏。 – Kamil