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']; // } ?> 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. 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] Link to comment https://forums.phpfreaks.com/topic/216236-multidimensional-arrays/#findComment-1123784 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.