2011-12-07 70 views
1

在Wordpress中,我試圖從頭開始創建一個metabox腳本,以便更好地理解Wordpress和PHP。PHP - 對於每個循環問題

雖然我對多維數組上的每個循環都有一些問題。我正在使用PHP5。

這是數組:

$meta_box = array();  
$meta_box[] = array(
      'id' => 'monitor-specs', 
      'title' => 'Monitor Specifications', 
      'context' => 'normal', 
      'priority' => 'default', 
      'pages' => array('monitors', 'products'), 
      'fields' => array(
       array(
        'name' => 'Brand', 
        'desc' => 'Enter the brand of the monitor.', 
        'id' => $prefix . 'monitor_brand', 
        'type' => 'text', 
        'std' => '' 
       ) 
      ) 
     ); 

這是每個循環:

foreach ($meta_box['pages'] as $post_type => $value) { 
      add_meta_box($value['id'], $value['title'], 'je_format_metabox', $post_type, $value['context'], $value['priority']); 
     } 

我試圖做的是遍歷鍵「頁」陣列中是'meta_box'數組內的數組,同時可以使用'meta_box'數組的鍵值。

我是否需要爲每個循環嵌套一些?

感謝一些指向正確的方向,所以我可以解決這個問題。

+0

你想用這些頁面中的項目做什麼? – Ikke

+0

如果$ meta_box描述了數組,您將獲得$ post_type 0,1並且爲空值$ ['context'],$ value ['priority']。但在你的方式應該是空數組,因爲$ meta_box ['pages']爲空 – Anthony

+0

'以更好地理解Wordpress和PHP.' [咆哮] WordPress的不是最好的代碼[#rans] –

回答

1
foreach ($meta_box[0]['pages'] as $post_type => $value) { 

$meta_box = array(... 
+0

這回答以及@ajreal的幫助我解決了這個問題!我最終使用了'$ meta_box = array(...' – Brigante

1

您的foreach$meta_box['pages']開頭,但沒有$meta_box['pages']

你有一個$meta_box[0]['pages'],因此你需要兩個循環:

foreach($meta_box as $i => $box) 
    foreach($box['pages'] as $page) 
     add_meta_box(.., ..); // do whatever 

什麼是你希望在你的$value變量?

1

這個位置:

$meta_box = array();  
$meta_box[] = array(...... 

表明,沒有$ meta_box [ '頁'。 meta_box是一個帶有數字索引的數組(檢查[]運算符),它的每個元素都是一個具有鍵'頁'的數組。

,所以你需要在$ meta_box使用的foreach,並且每個元素,你需要使用網頁鍵.. ID,標題,背景是在同一水平上的頁面元素,你可以看到

1

你正在引用了錯誤的數組鍵

$meta_box[] <-- $meta_box[0] 

但是,你參考使用: -

foreach ($meta_box['pages'] as $post_type => $value) { 

添加數組鍵就能解決問題: -

foreach ($meta_box[0]['pages'] as $post_type => $value) { 
1

也許可以創建一些類來保存這些信息。

class Metabox 
{ 
    public $id, $title, $context, $priority, $pages, $fields; 

    public function __construct($id, $title, $pages, $fiels, $context='normal', $priority='default') 
    { 
    $this->id = $id; 
    $this->title = $title; 
    $this->pages = $pages; 
    $this->fields = $fields; 
    $this->context = $context; 
    $this->priority = $priority; 
    } 

} 

$meta_box = array(); 

$meta_box[] = new Metabox(
    'monitor-specs', 
    'Monitor Specifications', 
    array('monitors', 'products'), 
    array(
    'name' => 'Brand', 
    'desc' => 'Enter the brand of the monitor.', 
    'id' => $prefix . 'monitor_brand', 
    'type' => 'text', 
    'std' => '' 
) 
); 

現在,您可以循環在meta_box陣列,如:

foreach ($meta_box as $box) 
{ 
    add_meta_box($box->id, $box->title, .. and more) 
    // This function could be placed in the metabox object 

    /* Say you want to access the pages array : */ 
    $pages = $box->pages; 

    foreach ($pages as $page) 
    { 
    .. 
    } 
} 

現在,你仍然有一個循環迴路,但也許有助於更清楚地看到你的問題。

+0

我需要學習如何創建類!這是我似乎無法讓我的大腦理解的事情之一......有一天希望它會使代碼非常簡單和漂亮! – Brigante

+0

在我住的地方,我們稱之爲「用顯微鏡敲打指甲」:) +1無論如何都是很好的解決方案 – k102