PHP:期望参数为给定的字符串数组

时间:2015-01-27 19:31:09

标签: php

我正在尝试逐行读取csv文件并将其内容保存在数组中。然后我使用foreach解析数组以打印每一行。

然而,当我尝试将变量(根据我应该是一个字符串)发送到deleteInstance方法时,它打印为数组而不是纯字符串。

我有问题发送到Softlayer API,因为它给我一个错误说字符串预期但是给出了数组?我不确定有什么问题

a.csv

7381838
7381840
7381842

PHP

   <?PHP
    require_once dirname(__FILE__) . '/SoftLayer/SoapClient.class.php';

    function readCSV($csvFile){
        $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle) ) {
            $line_of_text[] = fgetcsv($file_handle, 1024);
        }
        fclose($file_handle);
        return $line_of_text;
    }


    // Set path to CSV file
    $csvFile = 'a.csv';

    $csv = readCSV($csvFile);

    foreach ($csv as $value) {
        var_dump($value);
        print_r($value); 
        deleteInstance($value);

    }

    function deleteInstance($ccid){

        $apiUsername = 'xxxxx';
        $apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        $cancelRightNow = true; //or false if you want to wait till the billing cycle ends

       $cloudComputingInstanceId = $ccid; 
        print_r($cloudComputingInstanceId);
        var_dump($cloudComputingInstanceId);

$client = SoftLayer_SoapClient::getClient('SoftLayer_Virtual_Guest',    $cloudComputingInstanceId, $apiUsername, $apiKey);
$objectMask = new SoftLayer_ObjectMask();
$objectMask->billingItem;
$client->setObjectMask($objectMask);
$cci = $client->getObject();
$client = SoftLayer_SoapClient::getClient('SoftLayer_Billing_Item', $cci->billingItem->id, $apiUsername, $apiKey);
$billingItem = $client->getObject();
if ($billingItem != null) {
    if ($cancelRightNow) {
        $client->cancelService();
    } else {
        $client->cancelServiceOnAnniversaryDate();
    }
}
    }


    ?>

enter image description here

2 个答案:

答案 0 :(得分:2)

问题是你对deleteInstance的论证是一个数组......它看起来像这样:

$csv = Array(
   0 => Array(0 => '7381838'),
   1 => Array (0 => '7381840'),
   2 => Array(0 => '7381842')
)

这是因为您将其解析为CSV,它将每行拆分为基于分隔符的数组。这不是你想要的。而是使用file将每行读入数组:

function readCSV($csvFile){
    return file($csvFile);

}

$csv = readCSV('a.csv');

foreach ($csv as $value) {
    // your value is now the line of the file like '7381838'
    deleteInstance($value);

}

答案 1 :(得分:1)

试试看看会发生什么

foreach ($csv as $value) {
    $svalue = $value[0];
    var_dump($svalue);
    print_r($svalue); 
    deleteInstance($svalue);
}