1

I have the current recursive php funciton which actually uses curl (I've just simplifed the question here) and returns a 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);

I would like for the $data['data'] to contain my products that came from my curl response, now I have something like this:

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) "" }

The response that I am looking for is something like:

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) "" }
asked Feb 22, 2019 at 13:10
3
  • do you want to get all 5 products? Commented Feb 22, 2019 at 13:19
  • or you just want to convert response into your desired response. like this array(7) {[0] => string(8) "product1" [1] => string(8) "product2" ...} Commented Feb 22, 2019 at 13:19
  • I want to go through all the pages and get all the products and put all of them in $data['data'] Commented Feb 22, 2019 at 13:22

1 Answer 1

1

Hope this help. Edited

<?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);
?>
answered Feb 22, 2019 at 13:49
3
  • 1
    after carefully looking at what you did, it's not what i needed. you simply changed the input into an array and looped it. I am using curl to get my info, and the result is paginated. that's why i did all the if else page. that's why i need recursion. Commented Feb 24, 2019 at 8:11
  • i just did with recursion hope this help Commented Feb 24, 2019 at 9:22
  • 1
    yes, now it works, sorry about the first time, was too exited. thank you for all your help. Commented Feb 24, 2019 at 10:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.