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)
  • 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? – Adi Jul 01 '12 at 07: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. – biziclop Jul 01 '12 at 08:00
  • @AdnanShammout I am adding items one by one. i figured out the answer below by using array_merge instead.. –  Jul 01 '12 at 08:04
  • @LapMingLee, well now you know what to do in your next question :) – Adi Jul 01 '12 at 08:04

3 Answers3

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
);
Community
  • 1
  • 1
biziclop
  • 14,466
  • 3
  • 49
  • 65
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

slash197
  • 9,028
  • 6
  • 41
  • 70
Tomer
  • 17,787
  • 15
  • 78
  • 137
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
                  )
);
slash197
  • 9,028
  • 6
  • 41
  • 70