PHP-通过嵌套数组创建新数组,同时创建发布/图片关系

时间:2018-11-02 11:41:54

标签: php wordpress advanced-custom-fields

  • PHP 7.2
  • Wordpress 4.9.8
  • 高级自定义字段5.7.7

我有兴趣创建一个数组,其中每个项目都可以容纳:

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

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

WP帖子对象和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>

2 个答案:

答案 0 :(得分:1)

使用正确的$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>

答案 1 :(得分: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);