2012-08-11 149 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

相關問題