dqf67993 2013-05-15 22:37
浏览 33
已采纳

组合2个关联数组

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>';
  • 写回答

1条回答 默认 最新

  • doucitan2544 2013-05-15 23:28
    关注
    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab