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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.