简体   繁体   中英

How to retrieve data from multiple arrays

I have two arrays that was converted from csv file. The first csv line looks like this:

franchise_id,franchise_name,phone,website,email,region_codes;
1,"Abbott, Hackett and O`Conner",1-648-177-9510,auto-service.co/bw-319-x,Lupe-2485@auto-service.co,"36101,36055,36071";

The second csv line looks like this:

postal_code,region_code,city,state,region;

14410,36055,Adams Basin,NY,Monroe;

I converted these lines to arrays like this:

//Region Array    
    $region_lines = explode(PHP_EOL, $region_mappings_string);
    $region_array = array();
    foreach ($region_lines as $region_line) {
    $region_array[] = str_getcsv($region_line);
}
    //Franchise Array

    $franchise_lines = explode(PHP_EOL, $franchises_string);
    $franchise_array = array();
    foreach ($franchise_lines as $franchise_line) {
    $franchise_array[] = str_getcsv($franchise_line);
}

After that I got the result like this for Region:

Array
(
    [0] => Array
        (
            [0] => postal_code
            [1] => region_code
            [2] => city
            [3] => state
            [4] => region;
        )

[111] => Array
        (
            [0] => 14410
            [1] => 36055
            [2] => Adams Basin
            [3] => NY
            [4] => Monroe;
        )


 [112] => Array
        (
            [0] => 14617
            [1] => 36055
            [2] => Rochester
            [3] => NY
            [4] => Monroe;
        )

And for franchise:

 Array
(
    [0] => Array
        (
            [0] => franchise_id
            [1] => franchise_name
            [2] => phone
            [3] => website
            [4] => email
            [5] => region_codes;
        )

[1] => Array
    (
        [0] => 1
        [1] => Abbott, Hackett and O`Conner
        [2] => 1-648-177-9510
        [3] => auto-service.co/bw-319-x
        [4] => Lupe-2485@auto-service.co
        [5] => 36101,36055,36071;
    )

What I need to do is to look for 14410 postal code from the first array, get the region_code and look for this region_code in second array and then output the results in PHP. How can this be done?

Will have to loop over franchises array and run simple comparison

 function getDataByRegionCode($code, $franchiseArray) {
       foreach ($franchiseArray as $key => $data) {
           if(isset($data[5])){
              $codes = explode(',',$data[5]);
              if(in_array($code,$codes)) return $data;
           }
       }
       return NULL;
    }
    print_r(getDataByRegionCode(14410,$franchiseArray));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM