What I'm trying to accomplish is taking a simple array ($territories)
$territories = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
And assign each value as a variable, so that it can be used inside another function, and needs to run for each value in the first array.
The function being called is getting its data from another api, so that variable is required to correctly call the function.
Most of what I've done so far is working, except it's only running the function once. The function needs to run all 18 times, and return 18 separate arrays.
The complete end goal here is to check the user's current zipcode against each array, and then to perform a function if there's a match to each array.
Below is what I have so far. I've left out API credentials for confidentiality.
//get territories in Nitro API
function get_territories(){
$url = 'https://nitro.powerhrg.com/admin/api/v1/territories.json';
$token = '';
// Nitro API request via cURL
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: '. $token.'',
),
));
// store response as string and return it
$result = json_decode(curl_exec($curl), true);
curl_close($curl);
return $result;
}
//get zipcodes from territory ID in Nitro API
function get_zipcodes($territory_id){
$url = 'https://nitro.powerhrg.com/directory/api/v1/zip_codes.json?territory_id='.$territory_id;
$token = '';
$body = array(
'id' => $territory_id,
);
// Nitro API request via cURL
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: '. $token.'',
),
CURLOPT_POSTFIELDS => json_encode($body)
));
// store response as string and return it
$result = json_decode(curl_exec($curl), true);
curl_close($curl);
return $result;
}
// show JSON array of all territory data by id (testing)
function display_zipcodes_raw(){
// get list of territory IDs
$territories = get_territories();
$values = [];
foreach ($territories as &$territory) {
$values[] = $territory['id'];
$id = 0;
foreach ($values as $key=>$value) {
$id = $value;
}
$zipcodes = get_zipcodes($id);
$results = [];
foreach ($zipcodes as &$zip) {
$results[] = $zip['zip_code'];
}
return 'zipcode results: '.json_encode($results).'<br>';
}
}
add_shortcode( 'display_zipcodes_raw', 'display_zipcodes_raw' );
CodePudding user response:
Try the below
function display_zipcodes_raw()
{
$zipcodes = [];
foreach (get_territories() as $territory) {
$zipcodes[$territory['id']] = array_column(get_zipcodes($territory['id']), 'zip_code');
}
return json_encode($zipcodes);
}
As a recommendation, global functions that reference other global functions that serve a single responsibility like you have here should probably be collected together and placed into a class, object-orientated programming is truly a beautiful thing. Good luck on your adventure!
CodePudding user response:
You have l unnecessary variables and loops, it's much more simple:
// show JSON array of all territory data by id (testing)
function display_zipcodes_raw(){
// get list of territory IDs
$territories = get_territories();
$results = [];
foreach ($territories as $territory) {
$zipcodes = get_zipcodes($territory['id']);
foreach ($zipcodes as $zip) {
$results[] = $zip['zip_code'];
}
}
return 'zipcode results: '.json_encode($results).'<br>';
}