Php 如何从这个数组中获得正确的输出
我正在wordpress上使用一个自定义帖子,我试图以数组的形式获得我想要的输出,但它什么也没有显示 代码如下:Php 如何从这个数组中获得正确的输出,php,arrays,wordpress,multidimensional-array,Php,Arrays,Wordpress,Multidimensional Array,我正在wordpress上使用一个自定义帖子,我试图以数组的形式获得我想要的输出,但它什么也没有显示 代码如下: $idx = 0; $wp_query = new WP_Query(array( 'post_type' => 'book-products', 'posts_per_page' => -1, 'post_status'
$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,那么我可以在那里看到所有数据。我不是多维数组方面的专家,所以我确信存在一些问题 您的
id
在产品中在$idx中
$ids = array_column($output, 'id');
foreach ( $ids as $id ) {
echo $id;
}
您的id
位于产品中位于$idx
中
$ids = array_column($output, 'id');
foreach ( $ids as $id ) {
echo $id;
}
屏幕截图中的数组并不完全反映您在帖子中显示的$output
数组。我已经调整了代码。屏幕截图中的数组并不完全反映您在帖子中显示的$output
数组。我已经调整了密码。