user1794918
user1794918

Reputation: 1149

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.

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.

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

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)){

Upvotes: 0

Views: 564

Answers (1)

Bryan
Bryan

Reputation: 3494

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.

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

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

I hope that makes sense and clears it up a bit for you.

Upvotes: 1

Related Questions