php - 语法错误,意外的 T_DOUBLE_ARROW

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20365334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:07:10  来源:igfitidea点击:

php - syntax error, unexpected T_DOUBLE_ARROW

phpsyntax

提问by Andi Doank

how i can rid of this error??

我怎样才能摆脱这个错误?

Parse error: syntax error, unexpected T_DOUBLE_ARROW in /var/www/core/restvt.api.php on line 35

PHP Code :

PHP代码:

            $datax = Array();

    foreach ($inis as $key => $data){

        if ($data=="mem"){
            $str = number_format($ARRAY[(array_search($data.':',$ARRAY)+2)]/1024,0,',','.')." MB [ ".number_format(($ARRAY[(array_search($data.':',$ARRAY)+2)]/$ARRAY[(array_search($data.':',$ARRAY)+1)])*100,0,',','.')." % ]";
            array_push($datax, "mem"=>$str); //error here, why?
        }else{
        array_push($datax,$data=>$ARRAY[(array_search($data.':',$ARRAY)+1)]);
        }
    }

        $jsonr = json_encode($datax);

thx alot for your help...

非常感谢您的帮助...

回答by Robbie Averill

I hateseeing people use array_push - I know it's legal. In this case, you can't pusha key => valueto your array, just do this instead:

讨厌看到人们使用 array_push - 我知道这是合法的。在这种情况下,您不能akey => value送到您的数组,只需执行以下操作:

$datax['mem'] = $str;

Manual: http://php.net/manual/en/function.array-push.php

手册:http: //php.net/manual/en/function.array-push.php

edit

编辑

If you insist on using the array_pushtype method, you'll need to create a new array with your new key value pair then use array_mergeto join them:

如果您坚持使用array_pushtype 方法,则需要使用新的键值对创建一个新数组,然后使用array_merge加入它们:

$new_data = array('mem' => $str);
$datax = array_merge($datax, $new_data);

回答by James

The error is effectively:

错误是有效的:

unexpected '=>' (T_DOUBLE_ARROW)

意外的“=>”(T_DOUBLE_ARROW)

Which means PHP is not expecting those characters =>.
You can only use PHP predefined functions as they are intended, which you can find accurate documentation on php.net.
For your function see here: http://php.net/manual/en/function.array-push.php

这意味着 PHP 不期待这些字符=>
您只能按预期使用 PHP 预定义函数,您可以在 php.net 上找到准确的文档。
对于您的功能,请参见此处:http: //php.net/manual/en/function.array-push.php

You are trying to use the function in a way it was not intended, and so PHP throws an error as you performed something PHP does not allow.

您试图以非预期的方式使用该函数,因此当您执行 PHP 不允许的操作时,PHP 会抛出错误。

So you cannot use the function as you wish, and so need to approach it a different way.
This will work fine - appending a new value (in this case $str) to your array:

所以你不能随心所欲地使用这个函数,所以需要以不同的方式来处理它。
这将工作正常 - 将一个新值(在本例中$str)附加到您的数组中:

$datax['mem'] = $str;

Your array $dataxnow has the new key memwith the (new) value of whatever value is in $str.
Not only is this method more simple to manage, it has much less overhead as you are not using a function call - array_push().
Visiting the PHP manual page tells you this also.

您的数组$datax现在具有新键mem,其(新)值是 $str 中任何值
这种方法不仅更易于管理,而且由于您不使用函数调用,因此开销更少 - array_push()
访问 PHP 手册页也会告诉您这一点。

If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

如果您使用 array_push() 向数组添加一个元素,最好使用 $array[] = 因为这样就没有调用函数的开销。