1

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' );

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for such simplicity! Unfortunately, while it returns all available zipcodes, I need them to be returned in separate arrays and not combined into one array.
I've updated my question to add a bit more clarity, hopefully that helps.
That should do it :)
Me again - sorry! This does do what I want: [["11111","22222","33333"], ["44444","55555","66666"]] and so on, but can you help me make is so the ID from each $territory is added before each array as a variable, like this? $id1 = ["11111","22222","33333"], $id2 = ["44444","55555","66666"]] ... etc Looping through all these arrays is a little new to me and while I have the process down in my head, I can't get it out in my code LOL! I hope that all makes sense.
0

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>'; 
}

3 Comments

Where does $id get set in your code?
@Lothric thank you for the amazing clarity you've just shed, but it's not quite right -- while it does return all zipcodes available, I need them to be returned in separate arrays and not combined into one array.
@Lothric I've updated my question to add a bit more clarity, hopefully that helps.

Your Answer

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.