【问题标题】:Adding Multiple values to Cakephp3 Configure::write向 Cakephp3 Configure::write 添加多个值
【发布时间】:2016-05-05 19:45:29
【问题描述】:

我想做类似php的array_push

我正在使用 Cakephp3 Configure Class 并希望存储通知的用户 ID 列表。像这样:

Configure::write('Notified_Users', 1);
Configure::write('Notified_Users', 2);

但是值 2 会覆盖值 1。 有什么办法可以将数据推送到这个变量?然后稍后我可以检查所选用户是否在列表中。

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    你也可以这样创建数组

    Configure::write('Notified_Users.0', 1);
    Configure::write('Notified_Users.1', 2);
    

    或者干脆

    Configure::write('Notified_Users', [1, 2]);
    

    如果你debug(Configure::read('Notified_Users'));你会得到

    [
        (int) 0 => (int) 1,
        (int) 1 => (int) 2
    ]
    

    【讨论】:

      【解决方案2】:

      将数据推送到该变量:

      $notified_users = [];
      array_push($notified_users,1);
      Configure::write('Notified_Users', $notified_users);
      

      检查所选用户是否在列表中:

      if (in_array(1, Configure::read('Notified_Users')))
        {
        echo "Match found";
        }
      else
        {
        echo "Match not found";
        }
      

      【讨论】:

        【解决方案3】:

        试试这个:

        Configure::write('Notified_Users', [1, 2, 3]);
        

        Configure::write('Notified_Users', 
                [
                    '0' => 1,
                    '1' => 2,
                    '2' => 3
                ]
            );
        

        【讨论】:

          猜你喜欢
          • 2015-01-08
          • 2013-12-24
          • 2013-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-21
          • 2018-10-25
          相关资源
          最近更新 更多