2015-07-21 77 views
1

我试图将数组$jsonDataInArray写入到外部csv文件。现在,我的文件只有列标题,没有下面的数据。有人可以帮助我通过这个PHP数组,$jsonDataInArray,并将其写入外部.csv文件?将数组写入文件:PHP

//set url for pipedrive data being pulled 
$api_token="soemToken"; 
$url = "https://someURL.com; 

$ch = curl_init(); //initialize connection with a URL 
//check if cURL is enabled or not 
if(is_callable('curl_init')) 
{ 
    echo "curl_init Enabled"; 
} 
else 
{ 
    echo "curl_init Not enabled"; 
} 
echo '<br/><br/><br/><br/><br/><br/>'; 
curl_setopt($ch, CURLOPT_URL, $url); //fetching URL 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return queried data as string 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);//verify certificate 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);//check the existence of a common name & verify that it matches the hostname provided 
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/permissingFile.pem");//name of file holding certificates to verify peer with 
$json_response = curl_exec($ch);//perform cURL session. Returns ALL of JSON data if sucessful, false if not. 
$info = curl_getinfo($ch);//gets array of info about cURL transfer. 
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);//gets HTTP message about cURL transfer 
if ($status != 200) 
{ 
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch)); 
    //die(); 
} 
curl_close($ch);//close connection with URL 

// create an array from the data that is sent back from the API 
$response = json_decode($json_response, 1); 

// Gets the count of records returned from the api. Used in the for loop to go through response 1 array element at a time. 
$count = Count($response['data']); 

for ($x=0; $x<$count; $x++) 
{ 
    $currentRecord = $response['data'][$x]; 
    $jsonDataInArray = array 
    (
     "id" => $response['data'][$x]['id'], 
     "user_id" => $response['data'][$x]['user_id']['id'], 
     "person_id" => $response['data'][$x]['person_id']['value'], 
     "org_id" => $response['data'][$x]['org_id']['value'], 
     "stage_id" => $response['data'][$x]['stage_id'], 
     "title" => $response['data'][$x]['title'], 
     "value" => $response['data'][$x]['value'], 
     "currency" => $response['data'][$x]['currency'], 
     "add_time" => $response['data'][$x]['add_time'], 
     "update_time" => $response['data'][$x]['update_time'], 
     "stage_change_time" => $response['data'][$x]['stage_change_time'], 
     "active" => $response['data'][$x]['active'], 
     "deleted" => $response['data'][$x]['deleted'], 
     "status" => $response['data'][$x]['status'], 

    ); 
    ksort($currentRecord); 

} 

$test_array = $response['data'][0];//test_array = first row of data 

if($startPos == 0){ 
    $fp = fopen('cacheDeals3.csv', 'w'); 
    fputcsv($fp, array_keys($response['data'][0])); 
}else{ 
    $fp = fopen('cacheDeals3.csv', 'a'); 
} 


foreach ($jsonDataInArray as $fields) 
{ 
    fputcsv($fp, $fields); 
} 

回答

1

$ jsonDataInArray被覆盖上的for循环每次迭代,因此fputcsv被传递一个字符串作为$字段参数,而不是阵列。

您需要添加一个新的阵列$ jsonDataInArray每次更改 $jsonDataInArray = array

$jsonDataInArray[] = array