user1187135
user1187135

Reputation:

Array push error

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)

Upvotes: 1

Views: 5219

Answers (3)

Tomer
Tomer

Reputation: 17930

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

Upvotes: 1

biziclop
biziclop

Reputation: 14596

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
);

Upvotes: 1

slash197
slash197

Reputation: 9034

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
                  )
);

Upvotes: 0

Related Questions