【问题标题】:The array key is not the row ID value while using result_array in codeigniter在 codeigniter 中使用 result_array 时,数组键不是行 ID 值
【发布时间】:2015-09-10 22:48:25
【问题描述】:

在 Codeigniter 中获取数据库结果时,result_array 方法返回行数据,但数组键不是行 ID。出了什么问题?

型号:

<?php
class Admin_user extends CI_Model
{
    public function list_rows()
    {
        $query = array();
        $query = $this->db->get('content');
        return $query->result_array();
    }
}

查看:

<?php 
foreach ($get_all_content as $key => $values)
{
    $title = $values['title'];
    echo "<th>" . $key . "</th>";
}

控制器:

$data['get_all_content'] = $this->admin_user->list_rows(); 

print_r:

Array
(
    [0] => Array
        (
            [id] => 1
            [menu_id] => 12
            [title] => Register Domain name for free
            [sub_title] => This is the sub_title 
            [content] => this is the content description
            [description] => 
            [section] => 
        )
)

【问题讨论】:

  • print_r($data['get_all_content'];吗?你从哪里得到你的print_r()
  • 是的,它返回,我在视图 Array ( [0] => Array ( [id] => 1 [menu_id] => 12 [title] => 免费注册域名 [sub_title ] => 这是 sub_title [content] => 这是内容描述 [description] => [section] => ) )
  • 但是当你这样做$title = $values['title'];你得到了标题?
  • 是的!能够轻松获取标题值
  • 你的数据的key是0。

标签: php codeigniter


【解决方案1】:

试试这个:

 $yourdata = [0 => 
      ['id' => 1, 'menu_id' => 12, 'title' => 'Register Domain name for free','sub_title' => 'This is the subtitle', 'content' => 'this content', 'description' => 'description', 'section' => 'sect']
 ];

    foreach($yourdata as $key => $val)
    {
        foreach($val as $key2 => $newVal)
        {
            echo '<pre>';
            var_dump($key2, $newVal);
              //or echo it echo $key2 and echo $newVal
        }
    }

结果输出:

  string(2) "id"
    int(1)

  string(7) "menu_id"
    int(12)

  string(5) "title"
  string(29) "Register Domain name for free"

  string(9) "sub_title"
  string(20) "This is the subtitle"

  string(7) "content"
  string(12) "this content"

  string(11) "description"
  string(11) "description"

  string(7) "section"
  string(4) "sect"

【讨论】:

  • 它工作了 :-) 感谢您的代码示例有效,为什么第一个循环中的 $key 不起作用?
【解决方案2】:

这是一个老问题,但我现在回答是为了让新用户受益。

简短的回答是,当您使用result_array 检索数据库查询的结果时,您将获得一个数组数组。每一行都是它自己的数组,并且该行的键不是 ID 列。它只是一个序号,从零开始。

例如,如果我有这样的表:

+----+-----------+------------+
| id | username  | updated    |
+----+-----------+------------+
| 16 | nhageman  | 2019-10-29 |
| 20 | hnoland   | 2014-02-10 |
| 31 | lpostel   | 2020-04-08 |
| 36 | lemmerich | 2016-03-08 |
| 77 | rhegwood  | 2017-02-20 |
+----+-----------+------------+

...并像这样运行 Active Record 查询:

$results = $this->db
  ->get('users')
  ->result_array();

$results 数组将如下所示:

array (size=10)
  0 => 
    array (size=3)
      'id' => string '16' (length=2)
      'username' => string 'nhageman' (length=8)
      'updated' => string '2019-10-29' (length=19)
  1 => 
    array (size=3)
      'id' => string '20' (length=2)
      'username' => string 'hnoland' (length=7)
      'updated' => string '2014-02-10' (length=19)
  2 => 
    array (size=3)
      'id' => string '31' (length=2)
      'username' => string 'lpostel' (length=7)
      'updated' => string '2020-04-08' (length=19)
  3 => 
    array (size=3)
      'id' => string '36' (length=2)
      'username' => string 'lemmerich' (length=9)
      'updated' => string '2016-03-08' (length=19)
  4 => 
    array (size=3)
      'id' => string '77' (length=2)
      'username' => string 'rhegwood' (length=8)
      'updated' => string '2017-02-20' (length=19)

重点是:

请注意,每行的数组键是 0 到 4,而不是行 ID。

因此,当您像在问题中那样遍历数组时,您不会得到预期的结果:

// how to do it wrong
foreach ($results as $key => $row) {
    echo $key . ':';
    echo $row['username'] . '<br>';
}

错误的结果:

0: nhageman
1: hnoland
2: lpostel
3: lemmerich
4: rhegwood

您可以看到$key 只是分配给每一行的自动顺序键。大多数情况下,在处理 Active Record 结果时,您可以忽略该键,因为您要查找的行 ID 包含在 $row 数组中:

// how to do it right
foreach ($results as $row) {
    echo $row['id'] . ':';
    echo $row['username'] . '<br>';
}

正确结果:

16: nhageman
20: hnoland
31: lpostel
36: lemmerich
77: rhegwood

希望这可以为您澄清。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    相关资源
    最近更新 更多