duansen6750 2016-04-14 15:08
浏览 717

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

I get the error Fatal error: Cannot use [] for reading on line at return $array[]; inside the function.

I'm using PHP Version 7.0.1 on Windows 10.

I'm trying to get the output as follows:

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

, in json_encode

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

I previously used a different approach, but switched to this yesterday.

How can I get this work?

$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);

Below is how it was done previously, then changed to the above to avoid the .= parts. This approach works. See 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);
  • 写回答

1条回答 默认 最新

  • dongshou7903 2016-04-14 15:47
    关注

    You are returning $array[] instead of $array,

    Change this:

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

    into this:

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

    edit:

    This fixes it I guess:

    <?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);
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度