0

I am trying to read a csv file line by line and save its content in an array. I then parse the array using foreach to print each line.

However, when i try to send the variable(which according to me should be a string) to the deleteInstance method it prints as an array and not plain string.

I have issue sending this to Softlayer API since it throw me an error saying string expected but array given ? I am not sure what is wrong

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

6
  • The issue doesn't seem to be in the code you posted. More code - especially the bits that use functionality provided by SoapClient.class.php - would help. Commented Jan 27, 2015 at 19:33
  • 1
    The error has to do with the trim() function. It is never used in the code that you are showing us. We need to see the code where the function is used in order to help you. The error is quite self explanatory, though... Commented Jan 27, 2015 at 19:35
  • fgetcsv returns an array - so your readCSV is returning an array of arrays, not an array of string. Commented Jan 27, 2015 at 19:36
  • I assume youre trying to set a Parameter for his API call using one of the $lines_of_text which is an array parsed from the CSV. You need to show us your usage of the API. Commented Jan 27, 2015 at 19:36
  • thanks for your replies I have added the other half of the code. When i pass $cloudComputingInstanceId = "7381838"; it works fine. Commented Jan 27, 2015 at 19:36

2 Answers 2

2

The problem is your argument to deleteInstance is an array... It looks like this:

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

This is because you are parsing it as CSV which splits each line into an array based on a delimiter. This is not what you want. Instead use file to read each line into an array:

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);

}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this and see what happens

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

2 Comments

Notice array to string conversion error on that line. I suspect it is for some reason thinking the variable is still an array.
edited, this should work if it does indeed believe it is an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.