I have code where I'm simply calling an API and I've converted it to loop the call so that I can call it with three different argument values. This is because the API is structured where it can accept one value for that argument at a time, but I need to compare the results of the three calls and I think the best way is to build a single array from the responses by either pushing a value if it exists or pushing the whole index if not. Think of it like the API response can give me data that can be as much as 400 products that are sold across 3 stores. All 400 may be sold in one store, but maybe only 120 in another and 100 in the third.
I need my array to be all 400 products but I'll want to have an array within that holds the stores for the given product.
Code:
$IdentifierTest = 1472;
$arguments = ['store 1', 'store 2', 'store 3'];
$TestApi = app(TestApi::class);
$newArray = array();
foreach($arguments as $argument){
$testResponse = $TestApi->getData($IdentifierTest, $argument);
$Data = $testResponse->getResult()->getData(); // get the final results of call
//check if array empty, and if so, push all records
// if not empty, start checking 'id' against existing array records. If match, push stores. If not, add record to array
}
$this->makeFile($IdentifierTest, $Data);
An example of the response is:
array:426 [▼
0 => prices {#2698 ▼
#container: array:11 [▼
"prd" => 2380
"id" => "173489"
"price" => "65.00"
]
}
The issue is that specific example only shows up when calling the API with 'store 1' and 'store 2' as the argument, but not 'store 3'. What I'd like to do is call the API with each argument and create a new array with a ```stores``` index that pushes the store number if the id exists in the call, like so:
array:426 [▼
0 => prices {#2698 ▼
#container: array:11 [▼
"prd" => 2380
"id" => "173489"
"price" => "65.00"
stores: array[
'store 1',
'store 2'
]
]
}
So when it's called with 'store 1' I would push the response into an array with 'store 1' as the value in 'stores', then on 'store 2' I would take the response and first check to see if the id exists in the array I've made. If it exists, I'll add 'store 2' to the 'stores' and if not I'll make a new index in the array.
Basically, if product (id) 178293 is sold in all three stores then that means it would come back in all 3 API response calls and would end up being a single record in this new array with ```stores['store 1', 'store 2', 'store 3']```
How can I create a new array from this where I push only the stores if the id exists in the API call while keeping the current structure?