PHP / Wordpress:如何修改foreach()以特定的方式输出我的数组

时间:2016-02-05 18:44:54

标签: php arrays json wordpress foreach

主要编辑:重新构建问题,因为这可能更容易解决......

我正在尝试使用Wordpress中的PHP代码生成JSON微数据。我目前正在使用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相关代码

2 个答案:

答案 0 :(得分: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...

)

答案 1 :(得分:0)

只需删除$ i ++即可获得结束括号。 您定义$ i = 0 随着foreach的第一次迭代,它增加到1。 然后你分配$ array_details ['associatedMedia'] = $ post_details [$ i], 实际上是$ array_details ['associatedMedia'] [0] = $ post_details [1];

对不起我的“技术”英语。 (:

更新

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

...

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