如何从这个数组中获得正确的输出

时间:2017-01-28 00:53:25

标签: php arrays wordpress multidimensional-array

我在wordpress上使用自定义帖子,我试图将我想要的输出作为数组。但它没有显示任何内容

代码在这里:

   $idx = 0;
$wp_query = new WP_Query(array( 'post_type' => 'book-products',
                                    'posts_per_page' => -1,
                                    'post_status' => array('publish'),
                                    'tax_query' => array(
                                        'relation' => 'AND',
                                        array(  'taxonomy' => 'book-collection',
                                                'field'    => 'slug',
                                                'terms'    => $term),
                                        array(  'taxonomy' => 'book-style',
                                                'field'    => 'slug',
                                                'terms'    => $style,
                                                'operator' => 'NOT IN')
                                    )
                            )
                );

    while ($wp_query->have_posts()) : $wp_query->the_post();
        $id = get_the_ID();
        $color_t = wp_get_post_terms(get_the_ID(), 'book-product-color');
        $color = $color_t[0]->name;
        $color_code = strtoupper($color_t[0]->slug);


        if((is_array($output) && !array_key_exists($idx, $output)) || $output[$idx]['name'] != $style) {
        //if(!array_key_exists($idx, $output) || $output[$idx]['name'] != $style) {
            $idx++;
            $output[$idx] = array('name' => $style);
        }

        $output[$idx]['products'][] = array(    'id' => get_the_ID(),
                                                'style' => $style,
                                                'title' => get_the_title(),
                                                'color' => $color,
                                                'size' => $size,
                                                'lens' => $lens,
                                                'thumb_img' => wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'medium')[0],
                                                'front_img' => wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'full')[0],
                                                'profile_img' => MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'profile-image', get_the_ID(), 'full'),
                                                'stacked_img' => MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'stacked-image', get_the_ID(), 'large'),
                                                'cropped-front-image' => MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'cropped-front-image', get_the_ID(), 'full'),
                                                'cropped-side-image' => MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'cropped-side-image', get_the_ID(), 'full'),
                                                'content' => wp_strip_all_tags(get_the_content()),
                                                'post_type' => get_post_type());
    endwhile;
   //trying to output product data.need to get the ID as an example
foreach($output as $value)
{
echo $value['id'];
}

我也尝试将数据显示为json但不起作用。但是,如果我将数据检查为var_dumb,那么我可以在那里看到所有数据。我不是多维数组的专家,所以我确信存在一些问题。

1 个答案:

答案 0 :(得分:0)

id位于products内的$idx内。

    $ids = array_column($output, 'id');

    foreach ( $ids as $id ) {
        echo $id;
    }