在结果中包含数组中的数组

时间:2018-04-22 19:33:11

标签: php wordpress

我正在查询我的wordpress 4.9.5数据库,并希望返回一个如下所示的数组:

{
    "generalInfo": [
        {
            "post_id": 84,
            "title": "Test Title",
            "permalink": "www.link.com",
            "category": []
        },
        "Hardware": [
                {
                    "post_id": 12,
                    "title": "Part 1",
                    "permalink": "www.link.com",
                    "category": []
                },
                {
                    "post_id": 23,
                    "title": "Part 2",
                    "permalink": "www.link.com",
                    "category": []
                },
        ],
    ],
}

但是,目前我的数组如下所示:

{
    "generalInfo": [
        {
            "post_id": 84,
            "title": "Test Title",
            "permalink": "www.link.com",
            "category": []
        },
    ],
        "Hardware": [
                {
                    "post_id": 12,
                    "title": "Part 1",
                    "permalink": "www.link.com",
                    "category": []
                },
                {
                    "post_id": 23,
                    "title": "Part 2",
                    "permalink": "www.link.com",
                    "category": []
                },
        ],
}

我当前的api函数如下所示:

function allData()
{
    $hardwareQuery = new WP_Query(array(
        'posts_per_page' => -1,
        'post_type' => 'Hardware',
    ));

    $results = array(
        'generalInfo' => array(),
        'Hardware' => array(),
    );

    while ($hardwareQuery->have_posts()) {
        $hardwareQuery->the_post();

        $hardwarePostIds = json_decode(get_post_meta(get_the_ID(), 'neededHardware', true));

        $computerHardwareQuery = new WP_Query(array(
            'posts_per_page' => -1,
            'post_type' => 'Computer-Hardware',
            'post__in' => $hardwarePostIds,
        ));

        array_push($results['generalInfo'], array(
            'post_id' => get_the_ID(),
            'title' => get_the_title(),
            'permalink' => get_the_permalink(),
            'category' => get_the_category(),
        ));

        foreach ($computerHardwareQuery->posts as $item) {
            $shop = get_post_meta($item->ID, '_data_shop', true);
            $keys = array_keys($shop);

            array_push($results['rigHardware'], array(
                'partCategory' => 'x',
                'partTitle' => $item->post_title,
                'post_id' => $item->ID,
                'manufacturer' => $shop[$keys[0]]['manufacturer'],
                'img' => $shop[$keys[0]]['img'],
                'currency' => $shop[$keys[0]]['currency'],
                'price' => $shop[$keys[0]]['price'],
                'availability' => $shop[$keys[0]]['extra']['availability'],
            ));
        }
    }
    return $results;
}   

如您所见,我正在$results数组中设置数组。

有关如何在我的结果数组中包含Hardware - 数组的任何建议吗?

感谢您的回复!

1 个答案:

答案 0 :(得分:1)

你走在正确的轨道上,你几乎完成了它。只需将Hardware部件移动到一个函数并将该数组返回到generalInfo的创建中。我添加了一个检查以确保您的函数返回一个有效的响应,或者为Hardware返回一个空数组。

        array_push($results['generalInfo'], array(
            'post_id' => get_the_ID(),
            'title' => get_the_title(),
            'permalink' => get_the_permalink(),
            'category' => get_the_category(),
            'Hardware' => get_hardware($computerHardwareQuery),
        ));


// new fuction
function get_hardware($computerHardwareQuery){
    foreach ($computerHardwareQuery->posts as $item) {
            $shop = get_post_meta($item->ID, '_data_shop', true);
            $keys = array_keys($shop);

            array_push($results, array(
                'partCategory' => 'x',
                'partTitle' => $item->post_title,
                'post_id' => $item->ID,
                'manufacturer' => $shop[$keys[0]]['manufacturer'],
                'img' => $shop[$keys[0]]['img'],
                'currency' => $shop[$keys[0]]['currency'],
                'price' => $shop[$keys[0]]['price'],
                'availability' => $shop[$keys[0]]['extra']['availability'],
            ));
    }
    return isset($results)?$results?new array();
}
相关问题