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?
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):
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:
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