开发者

Script dies, I'm not sure why

开发者 https://www.devze.com 2022-12-30 02:25 出处:网络
I\'m trying to parse a 6,000 line 500 KB file into an array so I can import the data into our system.The problem is that the script stops executing somewhere between lines 3000-4000.There are no break

I'm trying to parse a 6,000 line 500 KB file into an array so I can import the data into our system. The problem is that the script stops executing somewhere between lines 3000-4000. There are no breaks in the code, we use it on other imports. Any ideas on why this might be happening and what I can do to prevent it?

/**
 * Takes a seperated value string and makes开发者_运维技巧 it an array
 * @param $delimiter string The delimiter to be seperated by, usually a comma or tab
 * @param $string string The string to seperate
 * @return array The resulting array
 */
public function svToArray ($delimiter, $string) {
    $x = 0;
    $rowList = array();
    $splitContent = preg_split("#\n+#", trim($string));
    foreach ($splitContent as $key => $value) {
        $newData = preg_split("#".$delimiter."#", $value);
        if ($x == 0) {
            $headerValues = array_values($newData);
        } else {
            $tempRow = array();
            foreach ($newData as $rowColumnKey => $rowColumnValue) {                                                
                $tempRow[$headerValues[$rowColumnKey]] = $rowColumnValue;
            }
            $rowList[] = $tempRow;
        }
        $x++;
    }
    return $rowList;
}

UPDATE: Error reporting is enabled. I've started using a file that's only 130KB at 1,500 lines and it does the same thing...

When I add debug code as in the following example nothing echoes at all unless I put an exit after the echo "test<br/>";

public function svToArray ($delimiter, $string) {
    $x = 0;
    $rowList = array();
    $splitContent = preg_split("#\n+#", trim($string));
    echo "test<br/>";
    foreach ($splitContent as $key => $value) {
        $newData = preg_split("#".$delimiter."#", $value);
        if ($x == 0) {
            $headerValues = array_values($newData);
        } else {
            $tempRow = array();
            foreach ($newData as $rowColumnKey => $rowColumnValue) {                                                
                $tempRow[$headerValues[$rowColumnKey]] = $rowColumnValue;
            }
            $rowList[] = $tempRow;
        }
        $x++;
    }
    echo "test";
    $this->tru->debug($rowList);
    exit;
    return $rowList;
}

UPDATE If I comment out $tempRow[] = $rowColumnValue; then it echoes everything fine....


Probably it just timeouts. Does it always stop after X seconds?

Try setting the max execution time higher: set_time_limit(900) at the top of your pages.

You can check the max execution time in your phpinfo(): 1. Create a new php page with 2. Search for max_execution_time


Have you looked at the files? Is there a line with too many delimiters? Also, what are all the "#" about?

My best guess is that you're hitting a line where $headerValues[$rowColumnKey] is not defined.


What about $rowColumnKey and $rowColumnValue ?

They are not defined in the function.


Make sure that you have error reporting set, put this on top of your php file:

ini_set('display_errors', true);
error_reporting(E_ALL);

Also you can extend the script's execution time:

ini_set('max_execution_time', 50000);


About the only time I've a php script die, with no output and no errors is when it runs out of memory. It doesn't look like your script would use much memory, but I would check to make sure that the memory limit for php is high enough.


Are you sure that you are iterating through the returned array instead of simply trying to print it? (The issue may be outside of the function rather than within it.)

Also...

`exit;
 return $rowList;`

Try removing the 'exit;' line and see if that changes anything.

Also, $splitContent is not defined as an array before being used, but preg_split is going to return an array. (This should not impact your results, but it is safe to do so.)

Why are you using $key => $value pairs when you cannot be sure of what will be within the string? If we could see an small example of the content of $string, we might be able to adjust this function to work better.

The # should be replaced with /, but that may simply be a formatting issue.

$newData should be defined as an array before being used just to be safe even though that is not causing your issue.


If you can, increase maximum execution time in php.ini.. also increase the maximum memory amount for each instance.. checking error logs of your webserver could also help.


It appears as though there was too much output to the output buffer, causing the page to show nothing at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消