如何解决致命错误:在这种情况下,无法使用[]进行读取


How can I solve Fatal error: Cannot use [] for reading in this case

我在函数内的return $array[];处得到错误Fatal error: Cannot use [] for reading on line

我使用的是Windows 10上的PHP 7.0.1版本。

我试图得到如下输出:

<i>Error Line One...</i><br>
<i>Error Line Two...</i><br>

,在json_encode

{"s":"Error Line One...<'/i>Error Line Two...<'/i>","success":false}

我以前使用了一种不同的方法,但昨天改为使用这种方法。

我怎样才能得到这份工作?

$errorCount = 1;
$errors[] = '*Error Line One...';
$errors[] = '*Error Line Two...';
$errors['success'] = ($errorCount == 0 ? True : False);
$errors[] = ajax($errors);
function ajax($array) {
    $array = preg_replace('#'*(.+?)(?![^*])#','<i>$1</i><br>',$array);
    return $array[];
}
json_encode($errors);

以下是以前的做法,然后改为上面的做法以避免.=部分。这种方法行之有效。请参见Fiddle。

$errorCount = 1;
$errors['s'] = '*Error Line One...';
$errors['s'] .= '*Error Line Two...';

$errors['s'] = ajax($errors);
function ajax($array) {
    $array = preg_replace('#'*(.+?)(?![^*])#','<i>$1</i>',$array);
    return $array['s'];
}
$errors['success'] = ($errorCount == 0 ? True : False);
echo json_encode($errors);

您返回的是$array[]而不是$array,

更改此项:

function ajax($array) {
    $array = preg_replace('#'*(.+?)(?![^*])#','<i>$1</i><br>',$array);
    return $array[];
}

进入这个:

function ajax($array) {
    return preg_replace('#'*(.+?)(?![^*])#','<i>$1</i><br>',$array);
}

编辑:

这修复了它,我想:

<?php
$errorCount = 1;
$errors[] = '*Error Line One...';
$errors[] = '*Error Line Two...';
$errors = ajax($errors);
$errors['success'] = ($errorCount == 0 ? True : False);
function ajax($array) {
    foreach ($array as $key => $value)
        $array[$key] = preg_replace('#'*(.+?)(?![^*])#','<i>$1</i><br>', $value);
    return $array;
}
echo json_encode($errors);
?>