Question

I have 2 arrays that i have created from 2 different systems:

URL changed

I need to loop through the array on the left and find a match for the business, address and zip. If there is a match on the right side then I need to grab the id and add it to the array item on the left.

I am structuring the arrays so i can change them as needed.

${'URL'} = 'http://reviewsfor.biz/api/biz/';

    // Initiate the cURL request
    $curl = curl_init();

    $data = array(
                    'api_key' => REVIEWS_API_KEY,
                    'format' => 'json',
                    'act' => 'active'
                   );

    // Set the cURL options
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    curl_setopt($curl, CURLOPT_URL, ${'URL'}.'?'.http_build_query($data)); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    // Execute the cURL POST        
    $response = curl_exec($curl);

    // Close the cURL connection
    curl_close($curl);

    $response = json_decode($response);

    // Out the list of businesses into an array
    ${'Business List'} = $response->businesses;
    ${'Local Biz Array'} = array();
    ${'Query'} = mysql_query('SELECT * FROM `companies`.`subdomains`');
    while(${'Sub Domain'} = mysql_fetch_assoc(${'Query'})){

        ${'Database'} = ${'Sub Domain'}['database'];
        ${'DB Query'} = mysql_query('SELECT * FROM `'.${'Database'}.'`.`users`');

        while(${'User'} = mysql_fetch_assoc(${'DB Query'})){
            if(${'User'}['company'] != ''){
                ${'Local Biz Array'}[] = array('business' => ${'User'}['company'], 'address' => ${'User'}['company_address'], 'zip' => ${'User'}['company_zip'], 'user_id' => ${'User'}['id']);
            }
        }

    }
    // Build an array
    ${'LBL Biz Array'} = array();
    foreach(${'Business List'} as $item){
        ${'LBL Biz Array'}[] = array('business' => $item->business->business, 'address' => $item->business->address, 'zip' => $item->business->zip, 'id' => $item->business->id);
    }


    echo '<div id="leftCol" style="float:left;width:49%;height:500px;overflow:scroll;">';
    echo '<pre>';
    print_r(${'LBL Biz Array'});
    echo '</pre></div>';

    echo '<div id="rightCol" style="float:right;width:49%;height:500px;overflow:scroll;">';
    echo '<pre>';
    print_r(${'Local Biz Array'});
    echo '</pre></div>';
Was it helpful?

Solution

function findMatch($arrayLeft, $arrayRight){
   for($i=0,$j=count($arrayLeft);$i<$j;$i++){
      for($k=0,$l=count($arrayRight);$k<$l; $k++){
         if($arrayLeft[$i]['business'] == $arrayRight[$k]['business'] && $arrayLeft[$i]['address'] == $arrayRight[$k]['address'] && $arrayLeft[$i]['zip'] == $arrayRight[$k]['zip']){
            $arrayLeft[$i]['user_id'] = $arrayRight[$k]['user_id'];
            unset($arrayRight[$k]); //remove addresses already matched
            break;      
         }     
      }
   }
   return $arrayLeft;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top