2011-11-02 51 views
1

我正在使用Wufoo api(https://{subdomain}.wufoo.com/api/v3/forms/{formIdentifier}/entries.{xml|json})嘗試將我所有的條目(當前爲150左右)作爲一個php數組。 Wufoo限制返回到100的條目數。所以現在我有兩個php數組,我想將它們連接/組合成一個數組。連接Wufoo API結果

到目前爲止的代碼:

$api_uri_1 = "https://example.wufoo.com/api/v3/forms/example-form/entries.json?pageStart=0&pageSize=100"; 
$api_uri_2 = "https://example.wufoo.com/api/v3/forms/example-form/entries.json?pageStart=1&pageSize=100"; 

function wufoo_api($api_uri) { 
    $curl = curl_init($api_uri); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_USERPWD, 'WUFOO-API-KEY-HERE:password'); 
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_USERAGENT, 'Wufoo Sample Code'); 
    $response = curl_exec($curl); 
    $resultStatus = curl_getinfo($curl); 

    if ($resultStatus['http_code'] == 200) { 
     $results = json_decode($response, true); 
     return $results; 
    } else { 
     $results = 'Call Failed '.print_r($resultStatus); 
     return $results; 
    } 
} 

$result1 = wufoo_api($api_uri_1); 
$result2 = wufoo_api($api_uri_2); 

我已經試過,也沒有工作

$all_results = array_merge($result1, $result2); 

和這樣的事情

$all_results = $result1; 
$all_results += $result2; 

如何CA n個I串聯/提前任何幫助

更新相結合RESULT1和RESULT2

感謝:什麼工作

感謝Ben我意識到我需要使用數組鍵爲目標的部分我需要的陣列。

$all_results = array_merge($result1['Entries'],$result2['Entries']); 
+0

請參閱下面我的答案中的意見,我不認爲你的更新是有道理的.. – Ben

+0

我糾正它,謝謝本。 – michaelespinosa

回答

2

json_decode的結果將包含根元素(根據api的條目)。

您將通過$ RESULT1 [ '項']訪問條目的陣列,所以來連接的條目,你需要做這樣的事情:

$all_results = array_merge($result1['Entries'],$result2['Entries']); 

$all_results = $result1['Entries']; 
$all_results += $result2['Entries']; 

也爲失敗的情況下,它應該是

$results = 'Call Failed '.print_r($resultStatus,true); 

(否則,你輸出print_r的結果,而不是返回它)

+0

謝謝Ben!我需要的**鍵**是將'['Entries']'添加到我的變量中。結果是'array_push($ result1 ['Entries'],$ result2 ['Entries'] [0]);'再次感謝! – michaelespinosa

+0

@michaelespinosa'array_push($ result1 ['Entries'],$ result2 ['Entries'] [0])'只會將$ result2中的_first_條目追加到$ result1的條目中......這聽起來不像你想要的做你的絕對正確 – Ben

+0

。謝謝 – michaelespinosa