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

如何将字典样式值添加到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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?