【问题标题】:How to retrieve a specific value from an array with keys and values in cakephp3如何在 cakephp 3 中使用键和值从数组中检索特定值
【发布时间】:2016-07-27 21:37:33
【问题描述】:

我正在尝试访问查询后形成的数组中的特定值。

$query = $views->find('all');
$results = $query->all();
$data = $results->toArray();
$results = $query->toArray();

echo $results[0];

回显显示{'id':1, 'date':2016-07-27,'amount':30}

我只想从金额中获得 30。我该怎么做呢?

【问题讨论】:

    标签: php cakephp cakephp-3.0 cakephp-3.x


    【解决方案1】:

    只需访问数组的属性

    echo $results[0]['amount']
    

    php docs on arrays

    【讨论】:

      【解决方案2】:

      获取单个项目:

      $query = $views->findById($id);
      //or
      $query = $views->get($id);
      
      echo $query->amount;
      

      或者如果你想全部获得它们

      $query = $views->find('all');
      
      foreach ($query as $view) {
          echo $view->amount;
      }
      

      【讨论】:

        猜你喜欢
        • 2021-06-30
        • 2018-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-15
        • 1970-01-01
        相关资源
        最近更新 更多