2014-06-11 44 views
-1

的兩側的兩個數組元素我有一個陣列,其看起來像:得到在一個特定的數組元素

myArray <- array 
    Image <- array 
     0 <- array 
      id - 850 
      name - someimagename 
     1 <- array 
      id - 763 
      name - alandscapepic 
     2 <- array 
      id - 567 
      name - imageofbuilding 
     3 <- array 
      id - 376 
      name - picname 

等。請注意,圖像的實際ID沒有任何特定的順序。

例如,當我查看id爲567的圖像時,我希望能夠知道image_id實際上位於數組中哪一側的信息。

編輯

我想array_search這樣的:

array_search(array('id'=>$id), $currentFolder['Image']);  

我怎樣才能做到這一點?

+0

因此,如果'Image'數組中該項目的關鍵是'2',你想知道如何訪問'1'和'3'鍵的項目?僅僅通過減去或增加鍵值「1」是否可以解決這個問題?你可以展示一些代碼,說明你目前正在試圖做這些事情,並指定具體的問題在哪裏? –

+0

我編輯了我的帖子 - 我無法弄清楚我如何得到我想要的id,因爲它不是像0 => 678,1 => 876等...它是0(id => 876),1( id => 876)etc ... –

+0

所以你的問題實際上是獲得你正在查看的項目的關鍵呢? –

回答

0

做一個函數來獲取子數組的索引和+或 - 1.例如:

$id=567; 

$index=false; 

foreach ($currentFolder['Image'] as $key=>$value){ 
    if (in_array($id, $value)) { 
    $index=$key; // <--- get the indexOf subarray 
    break; 
    } 
} 

echo $index; 
// if not false  
// $currentFolder['Image'][$index + 1]; 
// $currentFolder['Image'][$index - 1]; 
+0

謝謝,那就是它! –

+0

沒問題。需要提及的。 1.檢查索引== 0或索引== MAX_IN_YOUR_ARRAY。 2.你不能在子目錄中有多個567s,因爲這個函數將在第一個id => 567處停止。 @Zaphod Beeblebrox – VVLeon

相關問題