syntax error, unexpected T
This question already has an answer here:
I hate seeing people use array_push - I know it's legal. In this case, you can't push a key => value
to your array, just do this instead:
$datax['mem'] = $str;
Manual: http://php.net/manual/en/function.array-push.php
edit
If you insist on using the array_push
type method, you'll need to create a new array with your new key value pair then use array_merge
to join them:
$new_data = array('mem' => $str);
$datax = array_merge($datax, $new_data);
The error is effectively:
unexpected '=>' (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
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.
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:
$datax['mem'] = $str;
Your array $datax
now has the new key mem
with 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.
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.
链接地址: http://www.djcxy.com/p/69898.html上一篇: 使用href在PHP中添加一个链接
下一篇: 语法错误,意外的T