【问题标题】:PHP/Wordpress: how to amend foreach() to output my array in a specific wayPHP/Wordpress:如何修改 foreach() 以特定方式输出我的数组
【发布时间】:2016-02-05 18:44:53
【问题描述】:

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

我正在尝试在 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相关代码

【问题讨论】:

  • 你在哪里有$postsfrom?你能给我们var_dump($posts)的结果吗?
  • 全局 $post 变量与您所在的页面/帖子相关。如果您在存档页面上,请尝试查看 WordPress 循环和 WP_Query() 对象。 codex.wordpress.org/The_Loop
  • 我现在已将这部分代码包含在我的原始帖子中。这有帮助吗?

标签: php arrays json wordpress foreach


【解决方案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...
  )
)

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

)

【讨论】:

  • 谢谢你,Ravinder,请参阅上面我编辑的帖子。您的解决方案在中间的灰色框中给了我结果 - 所以我现在需要知道的是如何使索引号显示为 [associatedMedia] :)
  • 您不能拥有多个具有相同键的数组元素。数组键应该是唯一的。如果你想对每个数组元素使用相同的键,你最终会得到数组中的单个元素。
  • 我明白了。那么如何使用 'associatedMedia' 键创建多个数组,所以当我对 JSON 数据进行编码时,结果如下: "mainContentOfPage" => array( "@type" => "WebPageElement", "associatedMedia" = > array( 'image' => // info, 'name' => // info, 'url' => // info/ ), "associatedMedia" => array( 'image' => // info, 'name ' => // info, 'url' => // info ), // Etc.)
  • 我刚刚看到您更新了您的帖子。我会尝试一下您的建议,看看它们在 Google 结构化数据测试工具中的表现如何:)
  • 好吧,我刚刚在测试工具中测试了以下代码:$array_details['associatedMedia'][$i] = $post_details[$i]; i++; 它以某种方式准确地读取 JSON 微数据。问题解决了,非常感谢:)
【解决方案2】:

只需删除 $i++ 即可 foreach 右括号。 你定义 $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]

【讨论】:

  • 感谢 user2781327,您的回答还在我上面编辑的帖子中的中间灰色框中给出了结果 - 那么您知道如何将索引号替换为 [associatedMedia] 吗?快到了:)
猜你喜欢
  • 1970-01-01
  • 2020-06-10
  • 2012-12-18
  • 2012-04-18
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
  • 2010-10-07
  • 1970-01-01
相关资源
最近更新 更多