我遇到了array_push问题。它显示"Parse error: syntax error, unexpected T_DOUBLE_ARROW"
变量$ vars是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)
答案 0 :(得分:1)
=>
语法仅在定义数组时有效。 array_push
只能用于使用自动递增数字键推送元素。
也许您可以使用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
));
或者你可以使用+
运算符,认为它的行为与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
);
答案 1 :(得分:1)
来自php手册:
如果您要使用array_push()
插入"key" => "value"
配对成一个数组,可以使用以下方法完成:
$data[$key] => $value;
没有必要使用array_push
答案 2 :(得分:0)
您正在将数组推送到堆栈,因此您的代码应该反映出
array_push($vars, array(
'item_number'.$num => $id,
'item_name'.$num => $cart_item->name,
'amount_'.$num => $cart_item->discount_price,
'quantity_'.$num => $value
)
);