dongyu9894 2017-04-18 03:53
浏览 8
已采纳

php编辑for循环中的数组值

I have an array of arrays, and I want to loop them while assigning a new key-value for each. But the original array fails to response. Here is my try:

<?php
$cards = array(
    array(
        "test" => 1  
    ),array(
        "test" => 2      
    )
);

foreach($cards as $card){
    $card["success"] = 1;
}

print_r($cards);

OUTPUT:

Array
(
    [0] => Array
        (
            [test] => 1
        )

    [1] => Array
        (
            [test] => 2
        )

)

How can I modify the method hence the 'success' value can be inserted into each of them?

  • 写回答

2条回答 默认 最新

  • duanlingzei0170 2017-04-18 03:56
    关注

    Passing array elements by reference (notice the & sign):

    foreach($cards as &$card){
        $card["success"] = 1;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向