doutui2883 2014-01-26 14:23 采纳率: 100%
浏览 38

无法在CodeIgniter中向PHP数组添加数据

I have a PHP array, actually a MySQL row constructed with CodeIgniter's Active Record.

So I have an array which var_dumps like this :

array (size=10)
  0 => 
    array (size=4)
      'user_id' => string '2' (length=1)
      'puzzle_id' => string '17' (length=2)
      'birth' => string '2014-01-26 16:08:25' (length=19)
  1 => 
    array (size=4)
      'user_id' => string '2' (length=1)
      'puzzle_id' => string '16' (length=2)
      'birth' => string '2014-01-26 02:07:05' (length=19)
  2 => .....

this is constructed like this :

$this->db->order_by("birth" , "desc");
$rows = $this->db->get("my_table" , $limit)->result_array();        
foreach($rows as $row)
{
    $row['testindex'] = "testvalue";
}
return $rows;

so why does my array NOT have the "testindex" indices ?

Thanks for any help !

  • 写回答

3条回答 默认 最新

  • dsbezji539113152 2014-01-26 14:26
    关注

    Because that's not how PHP and foreach() in particular works.

    $row in your code is a copy of the corresponding element in $rows, not the actual element. Modifying a copy doesn't modify the original.

    You'd want to do this:

    for ($i = 0, $c = count($rows); $i < $c; $i++)
    {
        $rows[$i]['testindex'] = 'testvalue';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP