【问题标题】:PHP - creating a new array through nested arrays while creating a post/image relationshipPHP - 在创建帖子/图像关系时通过嵌套数组创建新数组
【发布时间】:2019-04-06 15:52:18
【问题描述】:
  • PHP 7.2
  • Wordpress 4.9.8
  • 高级自定义字段 5.7.7

我有兴趣创建一个包含每个项目的数组:

  • 帖子编号
  • 帖子标题
  • 属于帖子的图像数组

我为每个包含许多图像的帖子使用 ACF 转发器,转发器名称是 carousel。

WP po​​st 对象和 ACF 字段之间没有联系。

问题:

嵌套的 foreach 将所有图片推送到第一篇文章中。

预期:

嵌套的 foreach 将仅使用属于该帖子 ID 的图像填充 $randomArray。

$workshop_posts_args = array(
    'post_type' => 'workshops'
);

$randomArray = [
    'post_id' => '',
    'post_title' => '',
    'post_image_url' => []
];
$post_query = new WP_Query($workshop_posts_args);
if ($post_query->have_posts()) {
    while ($post_query->have_posts()) {
        $post_query->the_post();
        $carousel_array = get_field('carousel', get_the_ID());
        echo "<h2>".get_the_title()."</h2>";
        if ($carousel_array) {
            foreach ($carousel_array as $carousel_images) {
                foreach ($carousel_images as $image) {
                    $randomArray['post_id'] = get_the_ID();
                    $randomArray['post_title'] = get_the_title();
                    $randomArray['post_image_url'][] = $image['url'];
                    echo 'image_url:'.$image['url'].'<br>The array: <pre>'.print_r($randomArray, true).'</pre>';
                    ?>
                    <?php
                }
            }
        }
    }
}
?>
<h1>TOTAL ARRAY</h1>
<pre><?php print_r($randomArray) ?></pre>

【问题讨论】:

  • @AlivetoDie 这是结果:pastebin.com/SFZ7pZu6
  • 我遇到了您的问题并在 answer.check 中更新了我的代码

标签: php wordpress advanced-custom-fields


【解决方案1】:

您在循环中一次又一次地覆盖数组索引,这就是您的问题。

这样做:-

$randomArray = []; //before post_query

然后更改if 块,如下所示:-

if ($post_query->have_posts()) {
    while ($post_query->have_posts()) {
        $post_query->the_post();
        $id = get_the_ID();
        $randomArray[$id]['post_id'] = $id;
        $randomArray[$id]['post_title'] = get_the_title();
        $carousel_array = get_field('carousel', $id);
        if ($carousel_array) {
            foreach ($carousel_array as $carousel_images) {
                foreach ($carousel_images as $image) {
                    $randomArray[$id]['post_image_url'][] = $image['url'];
                    ?>
                    <?php
                }
            }
        }
    }
}

注意:- 其余代码将相同

以上代码将为您提供基于post-id 的多维数组。如果您希望索引为 0,1,2,3..... 格式,请执行以下操作:-

$randomArray = array_values($randomArray);

【讨论】:

  • 完美。现在我知道如何处理这些数组了,谢谢!
  • @clusterBuddy 很高兴为您提供帮助:):)
【解决方案2】:

使用正确的$randomArray 索引,如下所示:

<?php
  $workshop_posts_args = array(
  'post_type' => 'workshops'
);

$randomArray = array();
$post_query = new WP_Query($workshop_posts_args);
$index = 0;
if ($post_query->have_posts()) {
    while ($post_query->have_posts()) {
        $post_query->the_post();
        $randomArray[$index]['post_id'] = get_the_ID();
        $randomArray[$index]['post_title'] = get_the_title();
        $carousel_array = get_field('carousel', get_the_ID());
       //echo "<h2>".get_the_title()."</h2>";
       if ($carousel_array) {
        foreach ($carousel_array as $carousel_images) {
            foreach ($carousel_images as $image) {

                $randomArray[$index]['post_image_url'][] = $image['url'];
                //echo 'image_url:'.$image['url'].'<br>The array: <pre>'.print_r($randomArray, true).'</pre>';
                ?>
                <?php
            }
        }
    }
    $index++;
}
}
?>
<h1>TOTAL ARRAY</h1>
<pre><?php print_r($randomArray) ?></pre>

【讨论】:

    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 2020-06-26
    • 2017-11-30
    • 2013-01-19
    • 2021-12-15
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    相关资源
    最近更新 更多