#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    How to access array contents


    Hi,

    I want to know how to access the values of given array values using loop in php:

    Array
    (
    [0] => Array
    (
    [test1] => testval
    [test2] => testval2
    )

    i dont know what to loop and how to access it.

    Moreover if i am serializing the given array using php serialize function it is giving me output as N;. I dont know whats the problem with the serialize function of my code as i use

    serialize($this->['testing]);

    [testing] => Array
    (
    [0] => Array
    (
    [test1] => test.com
    [test2] => test2
    [test4] => test4

    )

    )

    Any help??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    I believe :

    foreach ($array as $k=>$v)
    {
    echo "Array element $k contains:<br>";
    foreach ($v as $k2=>$v2)
    echo "$k2 = $v2<br>";
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,074
    Rep Power
    536
    jimmyg999's solution will work.

    Also, use print_r() often for troubleshooting. Using echo('<pre>'.print_r($array,1).'</pre>'); will make it easier to read.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,904
    Rep Power
    6353
    Please post your actual code and use [ CODE ] and [ PHP ] tags properly so we can see the formatting and coloring more easily.

    Jimmy's solution solves your first question about the access of nested multidimensional variables.

    Your second question starts with invalid code and doesn't make much sense.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,170
    Rep Power
    22
    serialize($this->['testing]);
    If this is your exact line at error, one item missing is the second ' placed after the word 'testing'.

    But, as ManiacDan mentions, offer us more info such as these 3 things.
    1.) The code that builds your array, so way have in mind an idea of our own what it holds.
    1a.) Possibly a view of how the Array prints via NotionCommotion's: echo('<pre>'.print_r($array,1).'</pre>');
    2.) The exact line you are running which fails w/ an error.
    3.) The error.
    Last edited by Triple_Nothing; September 30th, 2013 at 11:59 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,904
    Rep Power
    6353
    off-topic discussion split to the lounge
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo