2013-05-15 105 views
0

我有我已經從2個不同的系統上創建2個陣列:結合2關聯數組

URL改變

我需要遍歷左邊的陣列,並找到一個匹配的業務,地址和郵編。如果右側有匹配,那麼我需要抓住id並將它添加到左側的數組項中。

我正在構建陣列,所以我可以根據需要更改它們。

${'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>'; 

回答

0
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; 
} 
+0

太謝謝你了! –