将数组写入文件:PHP

时间:2015-07-21 22:03:34

标签: php json

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

//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 个答案:

答案 0 :(得分:1)

在for循环的每次迭代中都会覆盖$ psonDataInArray,因此fputcsv作为$ field参数而不是数组传递一个字符串。

每次更新时都需要向$ jsonDataInArray追加一个新数组 $jsonDataInArray = array

$jsonDataInArray[] = array