dongren1986
2018-03-07 14:43
浏览 206
已采纳

如何将字典样式值添加到PHP数组?

I'm using PHP 5.4 and trying to add dictionary-style complex values to an array thusly:

array_push($qbProductsArray, $qbProduct => $authNetAmount);

I get this error:

Parse error: syntax error, unexpected '=>'

My desired result is to have a series of $qbProduct and $authNetAmount that are related together. I don't want to add them separately like this:

array_push($qbProductsArray, $qbProduct, $authNetAmount);

...because those 2 values are related to each other, and I don't want them just all thrown in together, even though they are adjacent. I want a firmer link between them. How can this be done?

图片转代码服务由CSDN问答提供 功能建议

我正在使用PHP 5.4并尝试将字典式复杂值添加到数组中:

  array_push($ qbProductsArray,$ qbProduct => $ authNetAmount); 
   
 
 

我收到此错误:

解析错误:语法错误,意外'=>'

我想要的结果是有一系列的 与代码相关的 $ qbProduct $ authNetAmount 。 我不想像这样单独添加它们:

  array_push($ qbProductsArray,$ qbProduct,$ authNetAmount); 
   
  
 

...因为这两个值彼此相关,我不希望它们全部被抛在一起,即使它们是相邻的。 我希望他们之间有更紧密的联系。 怎么办呢?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtanhe4607 2018-03-07 14:45
    已采纳

    try adding them as array:

    array_push($qbProductsArray, array($qbProduct => $authNetAmount));
    

    using the => syntax outside of the context of array is not possible in PHP.

    打赏 评论