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


PHP/Wordpress: how to amend foreach() to output my array in a specific way

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

我正在尝试使用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...
)

我的方法:

// 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相关代码

使用这个

$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...
  )
)

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...
)

只需删除 $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]