blinks Posted October 19, 2010 Share Posted October 19, 2010 I am having trouble displaying values in the multidimensional array at the bottom of this script. I can display the value of an element by specifying the sub-arrays e.g. echo $parsedxml['oaklistinfo']['contact']['contactname']; But surely there must be a way to specify an element without listing each of the sub-array names? Here's my code - <?php //Copyright Daniel FAIVRE 2005 - www.geomaticien.com function simplexml2array($xml) { if (get_class($xml) == 'SimpleXMLElement') { $attributes = $xml->attributes(); foreach($attributes as $k=>$v) { if ($v) $a[$k] = (string) $v; } $x = $xml; $xml = get_object_vars($xml); } if (is_array($xml)) { if (count($xml) == 0) return (string) $x; // for CDATA foreach($xml as $key=>$value) { $r[$key] = simplexml2array($value); } if (isset($a)) $r['@'] = $a; // Attributes return $r; } return (string) $xml; } $issn = $_GET['issn']; $baseurl = "http://www.oaklist.qut.edu.au/api/basic?query=".$issn; $xml = simplexml_load_file($baseurl); $parsedxml = simplexml2array($xml); print_r($parsedxml); echo "<br />"; //$i = 0; //for ($i = 0; i<count($parsedxml[$i]); $i++) { echo $parsedxml['oaklistinfo']['contact']['contactname']; echo "<br />"; echo $parsedxml['record']['copyright']['copyrightstatement']; // } ?> Quote Link to comment https://forums.phpfreaks.com/topic/216236-multidimensional-arrays/ Share on other sites More sharing options...
trq Posted October 19, 2010 Share Posted October 19, 2010 But surely there must be a way to specify an element without listing each of the sub-array names? Not really, that is how they are indexed. Quote Link to comment https://forums.phpfreaks.com/topic/216236-multidimensional-arrays/#findComment-1123778 Share on other sites More sharing options...
onlyican Posted October 19, 2010 Share Posted October 19, 2010 A trick I do when working with this is, saves referencing soo many indexes. [php $arrMyData = $parsedxml['oaklistinfo']['contact']; $strContactName = $arrMyData['contactname']; [/code] Quote Link to comment https://forums.phpfreaks.com/topic/216236-multidimensional-arrays/#findComment-1123784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.