2016-02-05 13 views
0

主要編輯:重新定義問題,因爲這可能會更容易解決......PHP/WordPress的:如何修改的foreach()輸出我的陣列以特定的方式

我試圖使用PHP代碼來生成JSON微觀數據在Wordpress中。我目前使用foreach()方法遍歷頁面上的帖子列表,將他們的縮略圖,標題和鏈接數據放入數組中,稍後我將該數組編碼爲JSON微數據。但是,我使用foreach()組裝的數組不會輸出我想要的數據。

我花了數小時試圖讓這部分數據輸出正確,但無濟於事。

-

我想實現(使用的print_r()來查看和測試我的PHP代碼) - 例如而不是索引號碼,如[0][1]等等,我想每個陣列輸出[associatedMedia]代替如下:

Array 
(
    [associatedMedia] => Array 
     (
      [image] => http://www.website.com/thumbnail.jpg 
      [name] => post title 
      [url] => http://www.website.com/the-post 
     ) 
    [associatedMedia] => Array 
     (
      [image] => http://www.website.com/second-thumbnail.jpg 
      [name] => second post title 
      [url] => http://www.website.com/the-second-post 
     ) 
    // And so on... 
) 

我現在的結果是:

Array 
(
    [0] => Array 
     (
      [image] => http://www.website.com/first-thumbnail.jpg 
      [name] => first post title 
      [url] => http://www.website.com/the-post-one 
     ) 

    [1] => Array 
     (
      [image] => http://www.website.com/second-thumbnail.jpg 
      [name] => second post title 
      [url] => http://www.website.com/the-post-two 
     ) 

    [2] => Array 
     (
      [image] => http://www.website.com/third-thumbnail.jpg 
      [name] => third post title 
      [url] => http://www.website.com/the-post-three 
     ) // And so on... 
) 

我的foreach方法:

// other PHP code 

global $post; 
global $wp_query; 

$category = $wp_query->get_queried_object(); 
$args = array('category' => $category->cat_ID); 
$posts = get_posts($args); 

$post_details = array(); 
$i = 0; 

foreach($posts as $post) { 
     setup_postdata($post); 
    $thumb_url = wp_get_attachment_image_src(get_post_thumbnail_id(),'thumbnail'); 
    $post_thumbnails['image'] = $thumb_url[0]; 
    $post_titles['name'] = get_the_title(); 
    $post_links['url'] = get_permalink(); 

    $post_details[$i]['image'] = $post_thumbnails['image']; 
    $post_details[$i]['name'] = $post_titles['name']; 
    $post_details[$i]['url'] = $post_links['url']; 
    $i++; 
}; 

wp_reset_postdata(); 

print_r($post_details); 

我只是開始進入更高級的編程,我相信我上面的代碼看起來很粗糙。所以任何幫助或提示如何我可以縮短它將不勝感激。

編輯:更多$post相關的代碼添加

+0

你在哪裏有'$ posts'from?你能給我們var_dump($ posts)'的結果嗎? – Tekay37

+0

全局'$ post'變量是相對於你所在的頁面/文章。如果您位於存檔頁面上,請嘗試查看WordPress Loop和「WP_Query()」對象。 https://codex.wordpress.org/The_Loop –

+0

我已經將我的代碼的這部分包含到了原始文章中。這有幫助嗎? – Sidders

回答

1

使用此

$array_details['associatedMedia'][$i] = $post_details[$i]; 

代替

$array_details['associatedMedia'] = $post_details[$i]; 

更新:

數組元素的鍵應該是唯一的。 使用以下任何格式來獲得所需的結果。

foreach($posts as $post) { 
    .... 
    $array_details['associatedMedia'][$i] = $post_details[$i]; 
    i++; 
} 

結果:

Array 
(
    ['associatedMedia'] => Array( 
     [0] => Array(
       [image] => http://www.website.com/first-thumbnail.jpg 
       [name] => first post title 
       [url] => http://www.website.com/the-post-one 
     ) 
     [1] => Array(
       [image] => http://www.website.com/second-thumbnail.jpg 
       [name] => second post title 
       [url] => http://www.website.com/the-post-two 
     ) 
     [2] => Array(
       [image] => http://www.website.com/third-thumbnail.jpg 
       [name] => third post title 
       [url] => http://www.website.com/the-post-three 
     ) // And so on... 
) 
) 

OR

foreach($posts as $post) { 
    .... 
    $array_details[$i]['associatedMedia']= $post_details[$i]; 
    i++; 
} 

結果:

Array 
(
    [0] => Array(
     ['associatedMedia'] => Array( 
       [image] => http://www.website.com/first-thumbnail.jpg 
       [name] => first post title 
       [url] => http://www.website.com/the-post-one 
      ) 
    ) 

    [1] => Array(
     ['associatedMedia'] => Array( 
       [image] => http://www.website.com/second-thumbnail.jpg 
       [name] => second post title 
       [url] => http://www.website.com/the-post-two 
     ) 
    ) 

[2] => Array(
     ['associatedMedia'] => Array( 
       [image] => http://www.website.com/third-thumbnail.jpg 
       [name] => third post title 
       [url] => http://www.website.com/the-post-three 
     ) 
    ) // And so on... 

) 
+0

謝謝Ravinder,看到我上面編輯過的帖子。你的解決方案給了我中間灰色框的結果 - 所以我現在需要找到的是如何使索引號顯示爲[associatedMedia]'而不是:) – Sidders

+0

你不能讓多個數組元素具有相同的鍵。數組鍵應該是唯一的。如果你想對每個數組元素使用相同的鍵,那麼你最終會得到數組中的單個元素。 –

+0

我明白了。那麼如何使用'associatedMedia'鍵創建多個數組,因此當我編碼JSON數據時,會出現如下所示: 「mainContentOfPage」=> array( 「@type」=>「WebPageElement」, 「associatedMedia」=>數組( '圖像'=> //信息, '名稱'=> //信息, 'URL'=> //方式/ ) 「associatedMedia」=>數組( ' image'=> // info, 'name'=> // info, 'url'=> // info ), //等 ) – Sidders

0

只需$ I ++中的foreach閉架移除。 你定義$ i = 0 用foreach它增加爲1 的第一次迭代然後分配$ array_details [ 'associatedMedia'] = $ post_details [$ i], 其實$ array_details [ 'associatedMedia'] [0] = $ post_details [1];

對不起,我的「技術」英語。 (:

UPDATE

foreach($posts as $key => $post) { 

...

$array_details[$key]['associatedMedia'] = $post_details[$i] 
+0

感謝user2781327,你的回答也給了我上面我編輯過的文章中的中間灰色框的結果 - 所以你知道我怎麼用'[associatedMedia]'替換索引號嗎?幾乎在那裏:) – Sidders

相關問題