15

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

3
  • "mem"=>$str <--- what's this? Commented Dec 4, 2013 at 1:53
  • variable string value from other array, i add for some string into it... Commented Dec 4, 2013 at 1:57
  • actually there only brackets missing.... array_push($datax, ["mem"=>$str]) should do it. Commented Feb 20, 2024 at 9:37

2 Answers 2

17

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);
Sign up to request clarification or add additional context in comments.

Comments

11

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.