Note that there are some explanatory texts on larger screens.

plurals
  1. POPhp - Create and display multidimensional array
    primarykey
    data
    text
    <p>I have this XML:</p> <pre><code>&lt;eSummaryResult&gt; &lt;DocSum&gt; &lt;Id&gt;11482001&lt;/Id&gt; &lt;Item Name="PubDate" Type="Date"&gt;2001 Jun&lt;/Item&gt; &lt;Item Name="EPubDate" Type="Date" /&gt; &lt;Item Name="Source" Type="String"&gt;Adverse Drug React Toxicol Rev&lt;/Item&gt; &lt;Item Name="AuthorList" Type="List"&gt; &lt;Item Name="Author" Type="String"&gt;Mantle D&lt;/Item&gt; &lt;Item Name="Author" Type="String"&gt;Gok MA&lt;/Item&gt; &lt;Item Name="Author" Type="String"&gt;Lennard TW&lt;/Item&gt; &lt;/Item&gt; &lt;Item Name="LastAuthor" Type="String"&gt;Lennard TW&lt;/Item&gt; &lt;Item Name="Title" Type="String"&gt;Adverse and beneficial effects of plant extracts on skin and skin disorders.&lt;/Item&gt; &lt;Item Name="Volume" Type="String"&gt;20&lt;/Item&gt; &lt;Item Name="Issue" Type="String"&gt;2&lt;/Item&gt; &lt;Item Name="Pages" Type="String"&gt;89-103&lt;/Item&gt; &lt;Item Name="LangList" Type="List"&gt; &lt;Item Name="Lang" Type="String"&gt;English&lt;/Item&gt; &lt;/Item&gt; &lt;Item Name="NlmUniqueID" Type="String"&gt;9109474&lt;/Item&gt; &lt;Item Name="ISSN" Type="String"&gt;0964-198X&lt;/Item&gt; &lt;Item Name="ESSN" Type="String" /&gt; &lt;Item Name="PubTypeList" Type="List"&gt; &lt;Item Name="PubType" Type="String"&gt;Journal Article&lt;/Item&gt; &lt;Item Name="PubType" Type="String"&gt;Review&lt;/Item&gt; &lt;/Item&gt; &lt;Item Name="RecordStatus" Type="String"&gt;PubMed - indexed for MEDLINE&lt;/Item&gt; &lt;Item Name="PubStatus" Type="String"&gt;ppublish&lt;/Item&gt; &lt;Item Name="ArticleIds" Type="List"&gt; &lt;Item Name="pubmed" Type="String"&gt;11482001&lt;/Item&gt; &lt;Item Name="eid" Type="String"&gt;11482001&lt;/Item&gt; &lt;Item Name="rid" Type="String"&gt;11482001&lt;/Item&gt; &lt;/Item&gt; &lt;Item Name="History" Type="List"&gt; &lt;Item Name="pubmed" Type="Date"&gt;2001/08/03 10:00&lt;/Item&gt; &lt;Item Name="medline" Type="Date"&gt;2002/01/23 10:01&lt;/Item&gt; &lt;Item Name="entrez" Type="Date"&gt;2001/08/03 10:00&lt;/Item&gt; &lt;/Item&gt; &lt;Item Name="References" Type="List" /&gt; &lt;Item Name="HasAbstract" Type="Integer"&gt;1&lt;/Item&gt; &lt;Item Name="PmcRefCount" Type="Integer"&gt;3&lt;/Item&gt; &lt;Item Name="FullJournalName" Type="String"&gt;Adverse drug reactions and toxicological reviews&lt;/Item&gt; &lt;Item Name="ELocationID" Type="String" /&gt; &lt;Item Name="SO" Type="String"&gt;2001 Jun;20(2):89-103&lt;/Item&gt; &lt;/DocSum&gt; &lt;/eSummaryResult&gt; </code></pre> <p>And i would like to create an array like this:</p> <pre><code>$results2 = array( '0' =&gt; array( 'id' =&gt; 'xxxxxxxx', 'authors' =&gt; array( '0' =&gt; 'xxxxxxxxxx', '1' =&gt; 'xxxxxxxxxx', ) ), '1' =&gt; array( 'id' =&gt; 'xxxxxxxx', 'authors' =&gt; array( '0' =&gt; 'xxxxxxxxxx', '1' =&gt; 'xxxxxxxxxx', ) ), ); </code></pre> <p>I have do this function to get data from XML to PHP in which i am trying to put the data with this structure and then display. I have not much experience with PHP i am having problems with syntax. This is the function:</p> <pre><code>function esummary_query($db, $id) { $context = stream_context_create(array( 'http'=&gt;array( 'user_agent' =&gt; 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11' ) )); $xml = file_get_contents('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=' . $db . '&amp;id=' . $id, FALSE, $context); $xml_file = simplexml_load_string($xml); $results2 = array(); foreach ( $xml_file-&gt;DocSum as $items ) { $results2[]['id'] = $items-&gt;Id; $authors = $xml_file-&gt;xpath("//Item[@Name='Author']"); foreach ($authors as $author) { $results2[]['authors'][] = $author; } } return $results2; } echo'&lt;h3&gt;id:&lt;/h3&gt;' . $results2[0]['id'] . "&lt;br&gt; author:" . $results2[0]['authors']; </code></pre> <p>But when i display it, the id is shown but the author's names don't. print_r display:</p> <pre><code>Array ( [0] =&gt; Array ( [id] =&gt; SimpleXMLElement Object ( [0] =&gt; 11850928 ) ) [1] =&gt; Array ( [authors] =&gt; Array ( [0] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [Name] =&gt; Author [Type] =&gt; String ) [0] =&gt; LoPresti PJ ) ) ) [2] =&gt; Array ( [authors] =&gt; Array ( [0] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [Name] =&gt; Author [Type] =&gt; String ) [0] =&gt; Hambrick GW Jr ) ) ) ) </code></pre> <p>Thanks everybody!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload