Accessing specific element in nested array

Discussion in 'PHP' started by khu84, Jan 1, 2010.

  1. #1
    I think my this question is much dumb one but need help here.

    I am accessing an svn directory via an API function, now it returns the result in variable and when I do print_r($result); it looks like following



    Now lets say I have to to access type index of element [2] I am using

    echo $result[2]['type'];

    but it don't show anything.

    So how to access specific elements if array looks like the above one?
     
    khu84, Jan 1, 2010 IP
  2. emed

    emed Peon

    Messages:
    70
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try $result[2]->type;
     
    emed, Jan 1, 2010 IP
  3. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well.. assuming it's not an object (and is an array), try this:

    echo $result['2']['type'];
    Code (markup):
    To make sure that it's an array, use this (if the above doesn't work):

    vardump($result);
    Code (markup):
     
    CodedCaffeine, Jan 2, 2010 IP
  4. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    simple remove single quotes

    echo $result[2][type];
     
    xenon2010, Jan 2, 2010 IP
  5. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Recreated his instance with variables:

    <?php
    
    # Set the questioned array.
    $result = array();
    $result[] = array(
    	'type' => 'directory',
    	'last-mod' => 'Tue, 08 Dec 2009 11:09:56 GMT',
    	'path' => '04100b1c-a44f-0410-922e-2fda5d045ff4',
    	'status' => 'HTTP/1.1 200 OK'
    );
    $result[] = array(
    	'type' => 'directory',
    	'last-mod' => 'Tue, 08 Dec 2009 11:09:56 GMT',
    	'path' => 'trunk',
    	'status' => 'HTTP/1.1 200 OK'
    );
    $result[] = array(
    	'type' => 'file',
    	'last-mod' => 'Sun, 15 Jun 2008 12:25:40 GMT',
    	'path' => 'trunk/xml2Array.php',
    	'status' => 'HTTP/1.1 200 OK'
    );
    
    # Echo the mass of debug information.
    echo '<pre>';
    print_r($result);
    
    echo "\n\n----------------------------------------\n\n";
    
    var_dump($result);
    
    echo "\n\n----------------------------------------\n\n";
    
    # Test each out.
    echo $result[2]['type']."\n";
    //echo $result[2][type]."\n"; // Notice:  Use of undefined constant type - assumed 'type' (but displays file)
    echo $result['2']['type']."\n";
    //echo $result[2]->type."\n"; // Notice:  Trying to get property of non-object
    
    echo '</pre>';
    PHP:
     
    CodedCaffeine, Jan 2, 2010 IP
  6. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Me the dumbest one, I was calling it in function which was defined on same page, but was not passing the data variable to it. Ahhh.. Me remained away from php for about 8 months to coldfusion but now gone mad ... hahhahahhahaha
     
    khu84, Jan 2, 2010 IP
  7. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Haha. We all make mistakes like that. Global is your friend in that case. xD
     
    CodedCaffeine, Jan 2, 2010 IP
  8. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    wow, using globals... i saw that about.. hmm... 5 years ago? :)
     
    insert, Jan 3, 2010 IP
  9. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    register_globals = On ?
     
    CodedCaffeine, Jan 3, 2010 IP
  10. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    i wanted to say that it's the dirty way...
     
    insert, Jan 7, 2010 IP