如何从嵌套循环创建单个数组?

How to Create single array from nested loop?

提问人:zeba puthawala 提问时间:12/7/2022 最后编辑:M. Erikssonzeba puthawala 更新时间:12/7/2022 访问量:44

问:

我只想要像这样的单个数组中的数据。我试图创建单个最终数组,但在属性中,我一直在获得相同的数据。

{
    "status": 200,
    "data": [
        {
            "city": "City 1",
            "property": [
                {
                    "id": 1,
                    
                },
                {
                    "id": 2,
                   
                },
            ]
        },
        {
            "city": "City 2",
            "property": [
                {
                    "id": 3,
                },
                {
                    "id": 2,
                    
                },
            ]
        },
        {
            "city": "City 3",
            "property": [
                {
                    "id": 18132,
                },
                {
                    "id": 17981,
                },
            ]
        }
    ]
}
 
$custom_terms = get_terms('property_state');

    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => 'property',
            'tax_query' => array(
                array(
                    'taxonomy' => 'property_state',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
         );

         $loop = new WP_Query($args);
         if($loop->have_posts()) {
            $city_name = $custom_term->name;
            while($loop->have_posts()) : $loop->the_post();
                $property[] =  array(
                   'id' => get_the_ID(),
                 );
            endwhile;
         }
            $final[] = array('city' =>$city_name, 'property' => $property);
    }

php wordpress 循环 嵌套

评论

0赞 CBroe 12/7/2022
为什么在任何地方没有正确初始化?$property
0赞 Vijay Hardaha 12/7/2022
显示您的预期输出以进行澄清。

答: 暂无答案