Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Accessing stdClass Object


    PHP Code:
    print_r($data['member_details']); //Prints:

    Array
    (
        [
    0] => stdClass Object
            
    (
                [
    email] => pmdg3@yahoo.com
                
    [password] => 6bc12817818952178a4446ef2fa1c01c
            
    )


    Why can't I access password like
    PHP Code:
    $data['member_details'][0]['password'
    I can get it by
    PHP Code:
    $data['member_details'][0]->password 
    which is strange code!
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    ...Because it's an object. See the part that says "stdClass Object"? That tells you it's an object. Which means you have to use object syntax.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by requinix
    ...Because it's an object. See the part that says "stdClass Object"? That tells you it's an object. Which means you have to use object syntax.
    No one cares what I like but I would have loved this
    PHP Code:
     $data['member_details'][0]->['password'
    Or
    PHP Code:
     print_r($data['member_details']); //Prints:

    Array
    (
        [
    0] => stdClass Object
            
    (
                
    email => pmdg3@yahoo.com
                password 
    => 6bc12817818952178a4446ef2fa1c01c
            
    )


  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    PHP Code:
     $data['member_details'][0]->['password'
    Not a fan. Looks like some weird hybrid of object and array syntax.

    Code:
    Array
    (
        [0] => stdClass Object
            (
                email => pmdg3@yahoo.com
                password => 6bc12817818952178a4446ef2fa1c01c
            )
    
    )
    Meh. And if you do that you might as well remove all the []s. I just don't see what's so confusing about "Array" and "stdClass Object".
  8. #5
  9. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    you have an array of objects

    Remember that arrays in PHP only have one dimension, but each value may be of any type, including an array (of other values, of any type, and so on)

    objects use the -> syntax and arrays use the [] syntax

    so $data['member_details'] is an array.The first element (zero indexed), $data['member_details'][0], is an object. So what about

    PHP Code:
    //
    $obj $data['member_details'][0];
    $email =  $obj->email;
    $password $obj->password
    or shorter and more explicit:

    PHP Code:
    //
    $email =  $data['member_details'][0]->email;
    $password $data['member_details'][0]->password
    Out of interest, where is this data coming from. I'm guessing DB/ORM fetch as object or jsonDecode or xml decoding...? some of these methods can be forced to return just arrays - check the docs
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  10. #6
  11. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by Northie
    Out of interest, where is this data coming from. I'm guessing DB/ORM fetch as object or jsonDecode or xml decoding...? some of these methods can be forced to return just arrays - check the docs
    Hi; Some docs
    PHP Code:
    $this->db->select('email, password, membership_exp, last_activity, date_joined, sex, d_dob, about, smoke, profile_visible, contact_visible, username, y_dob, id AS member_id');
                
    $this->db->from('members');
                
    $this->db->where('email'$email);
                
    $query $this->db->get();
                return 
    $query->result(); 
  12. #7
  13. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by Northie
    you have an array of objects
    Once you told me I need a paradigm shift, I still need it
  14. #8
  15. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Originally Posted by zxcvbnm
    Once you told me I need a paradigm shift
    You also need to read more - looking at the docs, you'll see this method - result_array(), just under where you linked to

    Originally Posted by http://ellislab.com/codeigniter/user-guide/database/results.html

    result_array()

    This function returns the query result as a pure array, or an empty array when no result is produced
    (i.e. instead of an array of objects)

    which means you could do

    PHP Code:
    $data['member_details'][0]['password'
    Last edited by Northie; August 14th, 2013 at 07:20 AM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  16. #9
  17. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by requinix
    ...Because it's an object. See the part that says "stdClass Object"? That tells you it's an object. Which means you have to use object syntax.
    Hi;

    Ok. How can we explain this?

    I was pretty sure $val->clicked should do it.

    Thanks
    html Code:
    Array
    (
        [0] => stdClass Object
            (
                [clicked] => y
                [title] => Let's move in together
            )
     
        [1] => stdClass Object
            (
                [clicked] => n
                [title] => I'm intrested in your Ad
            )
     
        [2] => stdClass Object
            (
                [clicked] => n
                [title] => I'm intrested in your Ad
            )
     
        [rows] => 3
    )
    <h4>A PHP Error was encountered</h4>
     
    <p>Severity: Notice</p>
    <p>Message:  Trying to get property of non-object</p>
    <p>Filename: controllers/account.php</p>
    <p>Line Number: 21</p>
    PHP Code:
    print_r($data['messages']);
                foreach(
    $data['messages'] as $val)
                    {
                        if(
    $val->clicked!='y')
                            {
                                ++
    $this->unread;    
                            }
                    } 
  18. #10
  19. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    Code:
    [rows] => 3
    That's not an object.
    PHP Code:
    unset($data['messages']['rows']); 
    before your loop.
  20. #11
  21. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by requinix
    Code:
    [rows] => 3
    That's not an object.
    PHP Code:
    unset($data['messages']['rows']); 
    before your loop.
    Ah, I see. Every element in the returned array MUST be object.


    I see there are 2 options in general. Which ine is better practise? Return as array of arrays or return as array of objects?
  22. #12
  23. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    Return an array of whatever so long as all the elements are the same type. Beyond that it depends on how easy things are.
  24. #13
  25. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    my preference is arrays - but that's probably because in my earlier days of development I was very comfortable using arrays at a time I had never used objects
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  26. #14
  27. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Originally Posted by zxcvbnm
    ... Every element in the returned array MUST be object.
    WILL be an object. Read the docs:

    This function returns the query result as an array of objects, or an empty array on failure.
    also note

    The above function is an alias of result_object()
    At a top level you will have an array. If the query was successful then every element WILL be an object.

    But if you use result_array() then at a top level you will have an array and if the query is successful the elements inside this array will also be arrays.

    Is there still any confusion?
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  28. #15
  29. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by Northie
    WILL be an object. Read the docs:
    Um, have you missed the whole discussion? The array contains the number of rows as an integer:

    PHP Code:
    [rows] => 
    And since that's not an object, he ran into trouble trying to access the clicked property.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo