通过数字索引获取ASSOC数组值 [英] Get ASSOC array value by numeric index

查看:82
本文介绍了通过数字索引获取ASSOC数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://www.codeigniter.com的所有Codeigniter查询示例中/user_guide/database/results.html ,我发现必须知道该字段的名称才能获取其值.

In all the examples of Codeigniter queries at https://www.codeigniter.com/user_guide/database/results.html, I find that the name of the field must be known to get its value.

$query = $this->db->query("SELECT title,name,body FROM table");

foreach ($query->result() as $row)
{
  echo $row->title;
  echo $row->name;
  echo $row->body;
}

例如,如果要获取title,我将执行row->title.有没有一种方法可以使用索引获取title,例如像$row[0]?

For example, if I want to get the title, I will perform row->title. Is there a way to get the title using an index e.g. like $row[0]?

推荐答案

使用result_array函数将查询结果作为纯数组返回

Use result_array function returns the query result as a pure array

$query = $this->db->query("SELECT title,name,body FROM table");
    $result = $query->result_array();
    foreach($result as $res){
      echo $res['title'];
      echo $res['name'];
      echo $res['body'];
    }

如果要通过索引访问,请使用array_values:

if you want to access via index then use array_values:

    $result = $query->result_array();
    foreach($result as $res){
      $r = array_values($res);
      echo $r[0];
      echo $r[1];
      echo $r[2];
    }

这篇关于通过数字索引获取ASSOC数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆