递归PHP函数,需要额外的步骤

时间:2019-02-22 13:10:33

标签: php recursion

我有当前的递归php函数,它实际上使用curl(我在这里简化了这个问题)并返回json。

<?php

function getData($params = array(), $page = 1) {
if ($page == 1) {
    $xmlString = '{"result":{"current_page":1,"total_pages":3,"products":[{"name":"product1"},{"name":"product2"}]}}';
} else if ($page == 2) {
    $xmlString = '{"result":{"current_page":2,"total_pages":3,"products":[{"name":"product3"},{"name":"product4"}]}}';
} else if ($page == 3) {
    $xmlString = '{"result":{"current_page":3,"total_pages":3,"products":[{"name":"product5"}]}}';
}


if ($params['supplier_id'] == 13) {
    $products = [];
    $xmlArray = json_decode($xmlString, true);
    if ($xmlArray['result']['current_page'] < 3) {
        $nextPage = (int) $page + 1;
        $products = array_merge($xmlArray['result']['products'], getData($params, $nextPage));

        return $products;
    }

    $xmlString = $products;
}

$data['data'] = $xmlString;
$data['discount'] = isset($params['discount)']) ? $params['discount'] : 0;
$data['connection_user'] = isset($params['connection_user']) ? $params['connection_user'] : '';

return $data;
}

$params = '{"id": 1, "supplier_id": 13}';
$params = json_decode($params, true);

$data = getData($params);
echo "<pre>";
var_dump($data);

我希望$ data ['data']包含来自我的卷曲响应的产品,现在我有了这样的东西:

array(7) { [0]=> array(1) { ["name"]=> string(8) "product1" } [1]=> array(1) { ["name"]=> string(8) "product2" } [2]=> array(1) { ["name"]=> string(8) "product3" } [3]=> array(1) { ["name"]=> string(8) "product4" } ["data"]=> array(0) { } ["discount"]=> int(0) ["connection_user"]=> string(0) "" }

我正在寻找的响应是这样的:

array(3) { ["data"]=> array(5) {  [0]=> array(1) { ["name"]=> string(8) "product1" } [1]=> array(1) { ["name"]=> string(8) "product2" } [2]=> array(1) { ["name"]=> string(8) "product3" } [3]=> array(1) { ["name"]=> string(8) "product4" } [4]=> array(1) { ["name"]=> string(8) "product5" } } ["discount"]=> int(0) ["connection_user"]=> string(0) "" }

1 个答案:

答案 0 :(得分:1)

希望此帮助。 已编辑

<?php

    function getData($params = array(), $page = 1) {
        $data['data'] = getProducts($params, $page);
        $data['discount'] = isset($params['discount)']) ? $params['discount'] : 0;
        $data['connection_user'] = isset($params['connection_user']) ? $params['connection_user'] : '';
        return $data;
    }
    function getProducts($params = array(), $page = 1){
        if ($page == 1) {
            $xmlString = '{"result":{"current_page":1,"total_pages":3,"products":[{"name":"product1"},{"name":"product2"}]}}';
        } else if ($page == 2) {
            $xmlString = '{"result":{"current_page":2,"total_pages":3,"products":[{"name":"product3"},{"name":"product4"}]}}';
        } else if ($page == 3) {
            $xmlString = '{"result":{"current_page":3,"total_pages":3,"products":[{"name":"product5"}]}}';
        }else{
            $xmlString = '{"result":{"products":[]}}';
        }

        $xmlArray = json_decode($xmlString, true);
        $products = $xmlArray['result']['products'];
        if ($params['supplier_id'] == 13) {
            if ($page <= 3) {
                $nextPage = (int) $page + 1;
                $products = array_merge($products, getProducts($params, $nextPage));
            }
        }
        return $products;
    }

    $params = '{"id": 1, "supplier_id": 13}';
    $params = json_decode($params, true);

    $data = getData($params);
    var_dump($data);
?>