2012-08-11 135 views
-3

嗨,大家好,我有这个脚本,但显然我不是使用的foreach正确我想知道是否有反正我可以作为一个我如何可以使用一个foreach PHP的一个foreach

$url = "http://api.website.com/1.0/country?source=ballsche&programid=5380&campaignid=100000"; 
$ch = curl_init(); 
$timeout = 0; 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
$rawdata = curl_exec($ch); 
curl_close($ch); 
$array = json_decode($rawdata,true); 
foreach($array as $obj) { 
    $country = $obj['country']; 
    $countrycode = $obj['countryCode']; 

} 
foreach($countrycode as $did) { 
    $wgurl = "http://api.website.com/1.0/city?source=ballsche&programid=5380&campaignid=100000&country-code=$did"; 
    $wgch = curl_init(); 
    $wgtimeout = 0; 
    curl_setopt($wgch, CURLOPT_URL, $wgurl); 
    curl_setopt($wgch, CURLOPT_HEADER, 0); 
    curl_setopt($wgch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($wgch, CURLOPT_CONNECTTIMEOUT, $wgtimeout); 
    $wgrawdata = curl_exec($wgch); 
    curl_close($wgch); 
    $wgarray = json_decode($wgrawdata,true); 
} 
foreach($wgarray as $wgobj) { 
    $city = $wgobj['city']; 
    $citycode = $wgobj['cityCode']; 
    if($city){ 
     $sql = "INSERT INTO `pt_city` (city, citycode) VALUES ('$city', '$citycode')"; 
     database_queryModify($sql,$result); 
     }else{ 
     echo "dint work"; 
    } 

} 

有这两个请求,结合工程必须是一个简单的方法来做到这一点iming从数据创建另一个数组,但我不能很好地得到它的权利,我一直得到错误ive试过这个和其他一些事情我的问题是我需要循环低谷县代码,并提出从代码有150个请求我需要循环低谷获取所有的城市信息,并为每个请求我需要解码JSON多数民众赞成它回来,并将其插入我的城市表

+2

-1对于代码甚至不遵循缩进的基本法则。 – Shubham 2012-08-11 12:38:14

+0

如果你不能说任何积极的东西,你从来没有听过什么修剪...... – dom 2012-08-11 12:43:43

+0

多米尼克,我建议你编辑你的问题,使其可读性。目前,这似乎是一个漫长的毫无意义的句子,这使得很难理解你实际要问什么。 – EyalAr 2012-08-11 12:44:14

回答

1

只是把foreach这样对彼此

$url = "http://api.website.com/1.0/country?source=ballsche&programid=5380&campaignid=100000"; 
$ch = curl_init(); 
$timeout = 0; 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
$rawdata = curl_exec($ch); 
curl_close($ch); 
$array = json_decode($rawdata,true); 
foreach($array as $obj) { 
    $country = $obj['country']; 
    $countrycode = $obj['countryCode']; 

    foreach($countrycode as $did) { 
     $wgurl = "http://api.website.com/1.0/city?source=ballsche&programid=5380&campaignid=100000&country-code=$did"; 
     $wgch = curl_init(); 
     $wgtimeout = 0; 
     curl_setopt($wgch, CURLOPT_URL, $wgurl); 
     curl_setopt($wgch, CURLOPT_HEADER, 0); 
     curl_setopt($wgch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($wgch, CURLOPT_CONNECTTIMEOUT, $wgtimeout); 
     $wgrawdata = curl_exec($wgch); 
     curl_close($wgch); 
     $wgarray = json_decode($wgrawdata,true); 

     foreach($wgarray as $wgobj) { 
      $city = $wgobj['city']; 
      $citycode = $wgobj['cityCode']; 
      if($city){ 
       $sql = "INSERT INTO `pt_city` (city, citycode) VALUES ('$city', '$citycode')"; 
       database_queryModify($sql,$result); 
      } else { 
       echo "dint work"; 
      } 
     } 
    } 
} 
+0

这是我尝试的第一件事,我总是得到一个无效的参数提供给foreach – dom 2012-08-11 12:51:08

相关问题