How can you push the sample_id value into the array using PHP DOM, along with first and samples?
I've tried adding another foreach loop, but I'm getting various notices such as
Trying to get property of non-object
XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<maintable>
<group>
<first>text1</first>
<second>text2</second>
<samples a="link" b="link">
<sample_id>000555</sample_id>
</samples>
</group>
<group>
<first>text3</first>
<second>text4</second>
<samples a="link" b="link">
<sample_id>111000</sample_id>
</samples>
</group>
</maintable>
PHP
<?php
$text = file_get_contents($file);
$content = iconv('UTF-8', 'UTF-8//IGNORE', $text);
$xml = new DOMDocument('1.0', 'ISO-8859-1');
$xml->loadXML($content);
$xpath = new DOMXPath( $xml );
$query = '/maintable/group[first="text3"]';
$nodes = $xpath->query( $query );
$var = array();
foreach( $nodes as $k => $node )
{
foreach( $node->childNodes as $cnode )
{
if ($cnode->nodeName == 'first')
{
$var[$k]["first"] = (string)$cnode->textContent;
}
if ($cnode->nodeName == 'samples')
{
$var[$k]["samp_url"] = (string)$cnode->getAttribute("a");
}
if ($cnode->nextSibling->nodeName == 'sample_id')
{
$var[$k]["sample_id"] = (string)$cnode->nextSibling->textContent;
}
}
}
//print_r($cnode->childNodes);
?>
Attempt to loop thru nodeList
$var = array();
$nodes = $xpath->evaluate('/maintable/group[first="text3"]');
foreach ($nodes as $node)
{
foreach( $node->childNodes as $cnode )
{
if (is_array($cnode))
{
foreach( $cnode->childNodes as $cn )
{
$var[]["sample_id"] = (string)$cn->textContent;
}
}
}
}
var_dump($var);