【问题标题】:rename key name codeigniter重命名键名codeigniter
【发布时间】:2020-09-12 09:48:21
【问题描述】:

数组

 Array
    (
        [1] => stdClass Object
            (
                [id] => 1
                [reg_type_id] => birth
                [mem_tbl_id] => 1
                [reg_date] => 1937-09-18
            )

        [2] => stdClass Object
            (
                [id] => 3
                [reg_type_id] => birth
                [mem_tbl_id] => 2
                [reg_date] => 1945-02-06
            )

        [3] => stdClass Object
            (
                [id] => 4
                [reg_type_id] => birth
                [mem_tbl_id] => 3
                [reg_date] => 1968-04-12
            )
    )

需要将“reg_date”重命名为“do_birth”
我尝试更改密钥,但出现“无法将 stdClass 类型的对象用作数组”错误。

foreach ($birth as $k => $v) {
            $birth[$k] ['do_birth'] = $birth[$k] ['reg_date'];
            unset($birth[$k]['reg_date']);
        }

上面几行代码对我不起作用!

【问题讨论】:

  • $birth[$k]->do_birth = $birth[$k]->reg_date;
  • 如果此信息来自数据库,您可以尝试在列上使用别名来消除重命名值的需要。

标签: php codeigniter


【解决方案1】:

您不能使用以下代码将对象作为数组获取

foreach ($birth as $k => $v) {
            $birth[$k]->do_birth = $birth[$k]->reg_date;
            unset($birth[$k]->reg_date);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2017-12-28
    相关资源
    最近更新 更多