0

I need this function to return an array. When I call the function it is printing the array, but when I use return $finalResult in the function, it is only printing the first array.


function readData($file)
{
    $finalResult = array();
    $inputText = file_get_contents($file);
    $textLines = explode("\n", $inputText);
    foreach ($textLines as $line)
    {
        $expLine = explode("\t", $line);
        if (count($expLine) < 8)
        {
            # The line does not have enough items, deal with error
            //echo "Item " . (isset($expLine[0]) ? $expLine[0]." " : "") . "ignored             because of errors\n";
            continue;
        }
        $finalResult = array(
            "title" => $expLine[0],
            "author" => $expLine[1],
            "isbn" => $expLine[2],
            "hardcover" => $expLine[3],
            "hc-quantity" => $expLine[4],
            "softcover" => $expLine[5],
            "sc-quantity" => $expLine[6],
            "e-book" => $expLine[7],
        );
        $arr = $finalResult;
        print_r($arr);
    }
}
1
  • Move the print_r outside of the loop } and then change it to return... Where you have $arr=$finalResult; use array merge to add them together Commented Sep 19, 2016 at 2:47

2 Answers 2

0

Hi You mush merge or push array to $finalResult see sammple

function readData($file){

    $finalResult = array();


    $inputText = file_get_contents($file);


    $textLines = explode("\n", $inputText);


    foreach($textLines as $line) {

    $expLine  = explode("\t", $line);

    if (count($expLine) < 8) {
        # The line does not have enough items, deal with error
        //echo "Item " . (isset($expLine[0]) ? $expLine[0]." " : "") . "ignored             because of errors\n";
        continue;
    }
    //Here []
    $finalResult[] = array(  
        "title" =>$expLine[0],
        "author"      => $expLine[1],
        "isbn"        => $expLine[2],  
        "hardcover"   => $expLine[3],
        "hc-quantity" => $expLine[4],
        "softcover"   => $expLine[5],
        "sc-quantity" => $expLine[6],
        "e-book"      => $expLine[7],

    );
    //$arr=$finalResult;
    //print_r($arr);        
    }
    return $finalResult;  
    }
Sign up to request clarification or add additional context in comments.

2 Comments

This returns an indexed array of arrays?
It return all array you are built.. You can print_r returned value and checkit out :)
-2

As described in my comment above

function readData($file){

    $arr         = array();
    $finalResult = array();
    $inputText   = file_get_contents($file);
    $textLines   = explode("\n", $inputText);

    foreach($textLines as $line) {
        $expLine  = explode("\t", $line);
        if (count($expLine) < 8) {
            # The line does not have enough items, deal with error
            //echo "Item " . (isset($expLine[0]) ? $expLine[0]." " : "") . "ignored             because of errors\n";
            continue;
        }
        $finalResult = array(
            "title" =>$expLine[0],
            "author"      => $expLine[1],
            "isbn"        => $expLine[2],  
            "hardcover"   => $expLine[3],
            "hc-quantity" => $expLine[4],
            "softcover"   => $expLine[5],
            "sc-quantity" => $expLine[6],
            "e-book"      => $expLine[7],
        );
        $arr=array_merge($arr, $finalResult);
    }

    return $arr;
}

Comments

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.