Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using the following code to change my array of data (comma seperated but not from a file) to an array that can be used. My code is as follows...

public function exportPartsAuthority($fileArray)
{       
    // Do whatever - sample code for a webservice request below.
    foreach ($fileArray as $filename => $fileContent) {

        // Do nothing

    }

    foreach(explode("\n",$fileContent) as $line){
        $item=explode(",",$line);
        file_put_contents('/home/apndev/public_html/output.txt', print_r($item, true));
    }

}

The values of $fileContent are seen below...

"100000002","flatrate_flatrate","1.0000","Brian","","","","","Sunrise","33323","Florida","US","","",
"100000002","flatrate_flatrate","1.0000","Brian","","","","","Sunrise","33323","Florida","US","","",
"100000003","flatrate_flatrate","1.0000","Brian","","","","","Sunrise","33323","Florida","US","2P-225","A1",

And this is how it's coming out in my file after exploding $fileContent...

Array
(
[0] => "100000002"
[1] => "flatrate_flatrate"
[2] => "1.0000"
[3] => "Brian"
[4] => ""
[5] => ""
[6] => ""
[7] => ""
[8] => "Sunrise"
[9] => "33323"
[10] => "Florida"
[11] => "US"
[12] => ""
[13] => ""
[14] => 
"100000002"
[15] => "flatrate_flatrate"
[16] => "1.0000"
[17] => "Brian"
[18] => ""
[19] => ""
[20] => ""
[21] => ""
[22] => "Sunrise"
[23] => "33323"
[24] => "Florida"
[25] => "US"
[26] => ""
[27] => ""
[28] => 
"100000003"
[29] => "flatrate_flatrate"
[30] => "1.0000"
[31] => "Brian"
[32] => ""
[33] => ""
[34] => ""
[35] => ""
[36] => "Sunrise"
[37] => "33323"
[38] => "Florida"
[39] => "US"
[40] => "2P-225"
[41] => "A1"
[42] => 
)

How would I go about producing each line from that string as its own array?

share|improve this question
    
your input data is flawed already, fix that first –  Populus Oct 7 '13 at 20:03
    
I've hidden a lot of data because it's personal information. How is it flawed otherwise? –  Brian Schroeter Oct 7 '13 at 20:20

1 Answer 1

You're actually really close, as you're already creating a new array for each line with your call to explode() -- you just need a variable to hold all the $items you're creating, and add $item in each iteration through your loop:

function exportPartsAuthority($fileArray) {       

    //this would hold your output
    $arrayOfArrays = array();

    foreach ($fileArray as $filename => $fileContent) {

        // Do nothing
        foreach(explode("\n",$fileContent) as $line){
            $item=explode(",",$line);
            file_put_contents('/home/apndev/public_html/output.txt', print_r($item, true));

            //now add it to your arrayOfArrays
            $arrayOfArrays[] = $item;
        }
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.