2013-11-15 29 views
0

你好可愛的人......訪問數組的第3級

我使用WordPress的高級自定義字段插件和I'n試圖找出如何從一個數組訪問某些值在數組中數組。

這裏是我的數組:

array(4) { 
    [0]=; 
    array(1) { 
    ["image"]=>; 
    array(10) { 
     ["id"]=> 
     int(282) 
     ["alt"]=> 
     string(12) "Cable bundle" 
     ["title"]=> 
     string(12) "Cable bundle" 
     ["caption"]=> 
     string(0) "" 
     ["description"]=> 
     string(0) "" 
     ["mime_type"]=> 
     string(9) "image/png" 
     ["url"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o.png" 
     ["width"]=> 
     int(1280) 
     ["height"]=> 
     int(960) 
     ["sizes"]=> 
     array(33) { 
     ["thumbnail"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o-220x150.png" 
     ["thumbnail-width"]=> 
     int(220) 
     ["thumbnail-height"]=> 
     int(150) 
     ["medium"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o-300x225.png" 
     ["medium-width"]=> 
     int(300) 
     ["medium-height"]=> 
     int(225) 
     ["large"]=> 
     string(92) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o-1024x768.png" 
     ["large-width"]=> 
     int(640) 
     ["large-height"]=> 
     int(480) 
     ["cat_post_thumb_sizecategoryposts-2"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o.png" 
     ["cat_post_thumb_sizecategoryposts-2-width"]=> 
     int(1280) 
     ["cat_post_thumb_sizecategoryposts-2-height"]=> 
     int(960) 
     ["cat_post_thumb_sizecategoryposts-3"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o-220x150.png" 
     ["cat_post_thumb_sizecategoryposts-3-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-3-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-4"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o.png" 
     ["cat_post_thumb_sizecategoryposts-4-width"]=> 
     int(1280) 
     ["cat_post_thumb_sizecategoryposts-4-height"]=> 
     int(960) 
     ["cat_post_thumb_sizecategoryposts-5"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o-150x150.png" 
     ["cat_post_thumb_sizecategoryposts-5-width"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-5-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-6"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o-220x150.png" 
     ["cat_post_thumb_sizecategoryposts-6-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-6-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-7"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o-220x150.png" 
     ["cat_post_thumb_sizecategoryposts-7-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-7-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-8"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o-220x150.png" 
     ["cat_post_thumb_sizecategoryposts-8-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-8-height"]=> 
     int(150) 
     ["home-page"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6773761367_bc13aec4a5_o-530x250.png" 
     ["home-page-width"]=> 
     int(530) 
     ["home-page-height"]=> 
     int(250) 
     } 
    } 
    } 
    [1]=> 
    array(1) { 
    ["image"]=> 
    array(10) { 
     ["id"]=> 
     int(281) 
     ["alt"]=> 
     string(7) "Circuit" 
     ["title"]=> 
     string(7) "Circuit" 
     ["caption"]=> 
     string(0) "" 
     ["description"]=> 
     string(0) "" 
     ["mime_type"]=> 
     string(10) "image/jpeg" 
     ["url"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b.jpg" 
     ["width"]=> 
     int(1024) 
     ["height"]=> 
     int(768) 
     ["sizes"]=> 
     array(33) { 
     ["thumbnail"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b-220x150.jpg" 
     ["thumbnail-width"]=> 
     int(220) 
     ["thumbnail-height"]=> 
     int(150) 
     ["medium"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b-300x225.jpg" 
     ["medium-width"]=> 
     int(300) 
     ["medium-height"]=> 
     int(225) 
     ["large"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b.jpg" 
     ["large-width"]=> 
     int(640) 
     ["large-height"]=> 
     int(480) 
     ["cat_post_thumb_sizecategoryposts-2"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b.jpg" 
     ["cat_post_thumb_sizecategoryposts-2-width"]=> 
     int(1024) 
     ["cat_post_thumb_sizecategoryposts-2-height"]=> 
     int(768) 
     ["cat_post_thumb_sizecategoryposts-3"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-3-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-3-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-4"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b.jpg" 
     ["cat_post_thumb_sizecategoryposts-4-width"]=> 
     int(1024) 
     ["cat_post_thumb_sizecategoryposts-4-height"]=> 
     int(768) 
     ["cat_post_thumb_sizecategoryposts-5"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b-150x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-5-width"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-5-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-6"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-6-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-6-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-7"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-7-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-7-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-8"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-8-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-8-height"]=> 
     int(150) 
     ["home-page"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/6500613825_911abf3799_b-530x250.jpg" 
     ["home-page-width"]=> 
     int(530) 
     ["home-page-height"]=> 
     int(250) 
     } 
    } 
    } 
    [2]=> 
    array(1) { 
    ["image"]=> 
    array(10) { 
     ["id"]=> 
     int(280) 
     ["alt"]=> 
     string(9) "TV cables" 
     ["title"]=> 
     string(9) "TV cables" 
     ["caption"]=> 
     string(0) "" 
     ["description"]=> 
     string(0) "" 
     ["mime_type"]=> 
     string(10) "image/jpeg" 
     ["url"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o.jpg" 
     ["width"]=> 
     int(1280) 
     ["height"]=> 
     int(800) 
     ["sizes"]=> 
     array(33) { 
     ["thumbnail"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o-220x150.jpg" 
     ["thumbnail-width"]=> 
     int(220) 
     ["thumbnail-height"]=> 
     int(150) 
     ["medium"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o-300x187.jpg" 
     ["medium-width"]=> 
     int(300) 
     ["medium-height"]=> 
     int(187) 
     ["large"]=> 
     string(92) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o-1024x640.jpg" 
     ["large-width"]=> 
     int(640) 
     ["large-height"]=> 
     int(400) 
     ["cat_post_thumb_sizecategoryposts-2"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o.jpg" 
     ["cat_post_thumb_sizecategoryposts-2-width"]=> 
     int(1280) 
     ["cat_post_thumb_sizecategoryposts-2-height"]=> 
     int(800) 
     ["cat_post_thumb_sizecategoryposts-3"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-3-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-3-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-4"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o.jpg" 
     ["cat_post_thumb_sizecategoryposts-4-width"]=> 
     int(1280) 
     ["cat_post_thumb_sizecategoryposts-4-height"]=> 
     int(800) 
     ["cat_post_thumb_sizecategoryposts-5"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o-150x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-5-width"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-5-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-6"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-6-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-6-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-7"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-7-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-7-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-8"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-8-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-8-height"]=> 
     int(150) 
     ["home-page"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/5249206215_be7e8572d8_o-530x250.jpg" 
     ["home-page-width"]=> 
     int(530) 
     ["home-page-height"]=> 
     int(250) 
     } 
    } 
    } 
    [3]=> 
    array(1) { 
    ["image"]=> 
    array(10) { 
     ["id"]=> 
     int(279) 
     ["alt"]=> 
     string(0) "" 
     ["title"]=> 
     string(11) "CAT5 Cables" 
     ["caption"]=> 
     string(0) "" 
     ["description"]=> 
     string(11) "CAT5 Cables" 
     ["mime_type"]=> 
     string(10) "image/jpeg" 
     ["url"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o.jpg" 
     ["width"]=> 
     int(1536) 
     ["height"]=> 
     int(2048) 
     ["sizes"]=> 
     array(33) { 
     ["thumbnail"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o-220x150.jpg" 
     ["thumbnail-width"]=> 
     int(220) 
     ["thumbnail-height"]=> 
     int(150) 
     ["medium"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o-225x300.jpg" 
     ["medium-width"]=> 
     int(225) 
     ["medium-height"]=> 
     int(300) 
     ["large"]=> 
     string(92) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o-768x1024.jpg" 
     ["large-width"]=> 
     int(640) 
     ["large-height"]=> 
     int(853) 
     ["cat_post_thumb_sizecategoryposts-2"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o.jpg" 
     ["cat_post_thumb_sizecategoryposts-2-width"]=> 
     int(1536) 
     ["cat_post_thumb_sizecategoryposts-2-height"]=> 
     int(2048) 
     ["cat_post_thumb_sizecategoryposts-3"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-3-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-3-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-4"]=> 
     string(83) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o.jpg" 
     ["cat_post_thumb_sizecategoryposts-4-width"]=> 
     int(1536) 
     ["cat_post_thumb_sizecategoryposts-4-height"]=> 
     int(2048) 
     ["cat_post_thumb_sizecategoryposts-5"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o-150x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-5-width"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-5-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-6"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-6-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-6-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-7"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-7-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-7-height"]=> 
     int(150) 
     ["cat_post_thumb_sizecategoryposts-8"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o-220x150.jpg" 
     ["cat_post_thumb_sizecategoryposts-8-width"]=> 
     int(220) 
     ["cat_post_thumb_sizecategoryposts-8-height"]=> 
     int(150) 
     ["home-page"]=> 
     string(91) "http://www.XXXXXXXX.com/dev/wp-content/uploads/2013/11/1543218761_62cea76758_o-530x250.jpg" 
     ["home-page-width"]=> 
     int(530) 
     ["home-page-height"]=> 
     int(250) 
     } 
    } 
    } 
} 

我需要存儲在["home-page"]每個陣列中的四個圖像的URL,因爲它是爲正確大小的圖像,顯​​然它在整個需要循環陣列。基本上,我需要了解如何訪問該數組的級別。 我已經做了一些閱讀,但我還是不太明白。 我需要多個foreach循環嗎?

到目前爲止,我有這樣的:

$images = get_field('homepage_image'); 

foreach ($images as $key => $image) { 
    foreach ($image as $subkey => $value) { 
     foreach ($value as $subsubkey => $subvalue) { 
      foreach ($subvalue as $subsubsubkey => $subsubvalue) { 
       $imageURL = $subsubvalue ['home-page']; 
         echo $imageURL; 
         echo "<br />";        } 
       } 
      } 
     } 

這似乎輸出字母h幾次。 誰能告訴我我做錯了什麼? 可能有更好的方法,但我想了解這一點。

乾杯 託比

+2

'$ array [0] ['image'] ['sizes'] ['home-page'];'您可以用第一個鍵的一個循環離開。 'foreach($ array as $ value){echo $ value ['image'] ['sizes'] ['home-page'];}' –

+0

確保您檢查循環中是否存在天氣,以避免uglay PHP錯誤: 如果(!array_key_exists('image',$ value)||!array_key_exists('sizes',$ value ['image'])||!array_key_exists('home-page',$ value ['image'] ['sizes'])){/ *處理錯誤* /} –

+1

邁克,關於你的故事將會在後面傳給後代。一個人如何犧牲了幾分鐘的時間來爲劣質的WordPress開發人員找到一個解決方案,以及該解決方案第一次如何完美運行。傳奇誕生了。 謝謝:) –

回答

2

如果你有一個多層次的數組,你可以參考使用所有密鑰的特定項目的一個電話:

$thumbnail_one = $array[0]['image']['sizes']['thumbnail']; 

在你的情況,你可以循環雖然只是第一級並在第一級訪問每個縮略圖。

$array = array(); //your array here 
foreach($array as $item) 
{ 
    $imageURL = $item['image']['sizes']['home-page']; 
    echo $imageURL, ' <br />'; 
} 
0

如果你需要避免手工申報項目,並添加了一點的「辣」,你可以從一些這樣開始

function search_key_recursive($ay, $search) 
{ 
    array_walk_recursive($ay, function($aItem, $aKey, $search) 
    { 
     if ($aKey == $search) 
     { 
      echo $aItem . '<br />'; 
     } 
    }, $search); 
} 

search_key_recursive($myArray, 'home-page'); 
1

第二版,甚至更好

function search_key_recursive($key, $array) 
{ 
    $result = array(); 

    foreach($array AS $k => $v) 
    { 
     if(is_array($v)) 
     { 
      $result = array_merge($result, search_key_recursive($key, $v)); 
     } 
     else 
     { 
      if($key === $k) 
      { 
       array_push($result, $v); 
      }   
     } 
    } 
    return $result; 
} 

$r = search_key_recursive('home-page', $myArray); 

var_dump($r);