简体   繁体   English

在Codeigniter中访问多维stdClass数组

[英]Accessing Multidimensional stdClass array in Codeigniter

I think I have a special case. 我想我有一个特例。 I think my array is formatted in such away that the conventional methods for accessing a stdClass are not going to work. 我认为我的数组格式如此之差,以至于传统的访问stdClass的方法都行不通。

Before someone dings me for not checking the posting (which there are many) on this subject. 在有人责骂我不检查有关该主题的帖子(有很多)之前。 I want to post that I have checked nearly everyone of them and here are a few links to prove it. 我想发布的是,我已经检查了几乎每个人,这里有一些链接可以证明这一点。

I have read them all. 我都看了。 I have tried all of the suggestions for accessing the data. 我已经尝试了所有有关访问数据的建议。 None of them has worked to day. 他们都没有工作到今天。

My data is formatted this way: 我的数据以这种方式格式化:

 Children::__set_state(array(
  'id' => NULL,
  'code' => NULL,
  'accld' => NULL,
  'fname' => NULL,
  'mname' => NULL,
  'lname' => NULL,
  'gender' => NULL,
  'dob' => NULL,
  'class' => NULL,
  'status' => NULL,
  'photo' => NULL,
  'allergies' => NULL,

'0' => 
  stdClass::__set_state(array(
 'id' => '1039',
 'code' => '2383',
 'accId' => '32',
 'fname' => 'Loren',
 'mname' => 'M',
 'lname' => 'Cings',
 'gender' => '2',
 'dob' => '2005-10',
 'class' => '1',
 'status' => '1',
 'photo' => ',
 'allergies' => '',
                     )),

'1' => 
  stdClass::__set_state(array(
 'id' => '1044',
 'code' => '6266',
 'accId' => '32',
 'fname' => 'Justin',
 'mname' => 'R',
 'lname' => 'Cings',
 'gender' => '1',
 'dob' => '2001-11',
 'class' => '15',
 'status' => '11',
 'photo' => ,
 'allergies' => '',
                     )),

My personal thoughts are that my data needs to be formatted like 我个人的想法是我的数据需要格式化为

    [0] => 
    stdClass::__set_state(array(  .....

Because when I look at all the other posted arrays. 因为当我查看所有其他发布的数组时。 The array number is in brackets verses single quotes. 数组编号在方括号和单引号之间。

In my model code, this is what is generating the array: 在我的模型代码中,这就是生成数组的原因:

   public function populate($row) {
    foreach ($row as $key => $value) {
        $this->$key = $value;
     }
  }

The only thing that pokes a hole in this theory is because I have another array that is generated by this same function and it can assess its values with $myarray->value. 唯一在这个理论上出现漏洞的事情是因为我有另一个数组是由相同的函数生成的,它可以使用$ myarray-> value来评估其值。

Thanks for looking and for your thoughts. 感谢您的查找和您的想法。

Answer 回答

I figured out the answer to this riddle. 我想出了这个谜的答案。 I had to go back and change my model code. 我不得不返回并更改我的模型代码。

From this: 由此:

public function accld($id) {

    $query = $this->db->get_where($this::DB_TABLE, array('accId' => $id));
    $result = $this->populate($query->result());
    return $result;

}

to this: 对此:

public function accld($id) {

    $query = $this->db->get_where($this::DB_TABLE, array('accId' => $id));

    $result = $this->populate($query->result('array'));
    return $result;

}

adding the 'array' argument converted the data returned from 添加'array'参数转换了从

what you see above to this 你在上面看到的

Children::__set_state(array(
 'id' => NULL,
 'code' => NULL,
 'accld' => NULL,
 'fname' => NULL,
 'mname' => NULL,
 'lname' => NULL,
 'gender' => NULL,
 'dob' => NULL,
 'class' => NULL,
 'status' => NULL,
 'photo' => NULL,
 'allergies' => NULL,
'0' => 
  array (
         'id' => '1161',
         'code' => '1784',
         'accId' => '124',
         'fname' => 'Diane',
         'mname' => '',
         'lname' => 'Gre',
         'gender' => '1',
         'dob' => '2012-6',
         'class' => '18',
         'status' => '7',
         'photo' => 
      ));

Then I could just iterate through it with a 然后我可以用一个

   foreach($children as $key => $val){ if(!empty($val)){

Although you figured out how to make it work, I'm not sure you're understanding why. 尽管您知道如何使它起作用,但我不确定您是否理解原因。 Codeigniter returns an array of objects unless you specify it to return an array. Codeigniter返回一个对象数组,除非您指定它返回一个数组。

For example if you received an array like 例如,如果您收到类似

$result = array(array('id'=>1,'name'=>'Steve','age'=>22) );

Using the 'array' argument, without it the standard code igniter return could be accessed with object notation in its default format, for example - you would access the name in the first result row like this 使用'array'参数,没有它,可以使用默认格式的对象符号访问标准代码点火器返回,例如-您可以像这样在第一个结果行中访问名称

$name = $result[0]->name;

I hope that makes sense and clears it up a bit for you. 我希望这很有意义,并为您解决了一些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM