1

I am having trouble with array_push. It is displaying "Parse error: syntax error, unexpected T_DOUBLE_ARROW" The variable $vars is an associative array for paypal.

array_push(
    $vars,
    'item_number' . $num => $id,
    'item_name' . $num => $cart_item->name,
    'amount_' . $num => $cart_item->discount_price,
    'quantity_' . $num => $value
);
$vars = array (
    'cmd' => '_cart',
    'charset' => 'utf-8',
    'upload' => '1',
    'currency_code' => 'HKD',
    'amount' => $_SESSION['total'],
    'custom' => $user_data->id
);
4
  • 1
    I really don't know what you're doing! Are you pushing an array onto $vars? or are you adding the items of the new array to $vars one by one? Commented Jul 1, 2012 at 7:54
  • BTW, why are you overwriting $vars in your second statement? What should be the sctructure of $vars? Maybe you could give a link to some documentation of the expected format. Commented Jul 1, 2012 at 8:00
  • @AdnanShammout I am adding items one by one. i figured out the answer below by using array_merge instead.. Commented Jul 1, 2012 at 8:04
  • @LapMingLee, well now you know what to do in your next question :) Commented Jul 1, 2012 at 8:04

3 Answers 3

1

The => syntax is only valid when you define an array. array_push can only be used to push elements with auto-incrementing numeric keys.

Maybe you could use array_merge: http://www.php.net/manual/en/function.array-merge.php

$vars = array_merge( $vars, array(
    'item_number'.$num => $id,
    'item_name'.$num => $cart_item->name,
    'amount_'.$num => $cart_item->discount_price,
    'quantity_'.$num => $value
));

Or you could use the + operator, thought it behaves quite differently from array_merge: + operator for array in PHP?

$vars =  $vars + array(
    'item_number'.$num => $id,
    'item_name'.$num => $cart_item->name,
    'amount_'.$num => $cart_item->discount_price,
    'quantity_'.$num => $value
);
Sign up to request clarification or add additional context in comments.

Comments

1

from php manual:

If you're going to use array_push() to insert a "key" => "value" pair into an array, it can be done using the following:

$data[$key] => $value;

It is not necessary to use array_push

Comments

0

You are pushing an array to the stack so your code should reflect that

array_push($vars, array(
                     'item_number'.$num => $id,
                     'item_name'.$num => $cart_item->name,
                     'amount_'.$num => $cart_item->discount_price,
                     'quantity_'.$num => $value
                  )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.