Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - syntax error, unexpected T_DOUBLE_ARROW [duplicate]

Tags:

syntax

php

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 :

            $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...

like image 932
Andi Doank Avatar asked Dec 04 '13 01:12

Andi Doank


1 Answers

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.

like image 81
James Avatar answered Sep 25 '22 20:09

James