Jump to content

Multidimensional arrays


blinks

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.