开发者

php recursion - notice: undefined offset: 20 array

I'm working to parse the following string but at the same time trying to get rid of the current undefined offset on the index array. I would appreciate some help w/ the undefined offset issue.

error_reporting(E_ALL);
ini_set('display_errors', '1');
function my_recursion($String, &$Inc) {
    $l = strlen($String);
    $has_quotes = 0; $array = array();
    $x= 0;  
    for ($Inc; $Inc < $l; $Inc++) {
        $my_char = $String[$Inc];
        if ($my_char == '(' && !$has_quotes) {
            $Inc++;开发者_如何转开发
            $array[$x] = my_recursion($String, $Inc);
            $x++;
        } else if ($my_char == '"') {
            $has_quotes = !$has_quotes;
            if (!$has_quotes)
                $x++;
        } else if ($has_quotes) {
            $array[$x] .= $my_char;
        }   
    }   
    print_r($array);
}
$String = '(("HELLO"("BAR")("FOO")()""))';
$Inc = 0;
(my_recursion($String, $Inc));


To get rid of the errors, add this line to the start of your function:

$array = array();

and replace this line:

$array[$x] .= $my_char;

with this:

$array[$x] = isset($array[$x])? $array[$x].$my_char : $my_char;

For help with your recursion, you'll need to describe its desired behavior.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜