json 循环并为数组中的每个值分配变量,然后为每个变量运行函数

vaj7vani  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(117)

我尝试完成的是取一个简单的数组($territors)$territories = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]并将每个值赋值为一个变量,这样它就可以在另一个函数中使用,并且需要为第一个数组中的每个值运行。
被调用的函数正在从另一个API获取其数据,因此需要该变量才能正确调用该函数。
到目前为止,我所做的大部分工作都是有效的,除了只运行一次函数,函数需要运行全部18次,并返回18个独立的数组。
这里完整的最终目标是针对每个数组检查用户的当前邮政编码,如果每个数组都匹配,则执行一个函数。
以下是我目前所拥有的,为了保密,我省略了API凭据。

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

z9gpfhce1#

尝试以下方法

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

作为一个建议,引用其他全局函数的全局函数,像你在这里所做的那样,应该被收集在一起,并放置到一个类中,面向对象编程确实是一件美好的事情。祝你好运!

luaexgnf

luaexgnf2#

你有l个不必要的变量和循环,这就简单多了:

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

相关问题