I have sparql output in XML like this : `
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="Keyword2"/>
</head>
<results ordered="false" distinct="false">
<result>
<binding name="Keyword2">
<uri>http://www.semanticweb.org/g/ontologies/2010/0/OntologiBatikTanty#Soga_Jawa</uri>
</binding>
</result>
<result>
<binding name="Keyword2">
<uri>http://www.semanticweb.org/g/ontologies/2010/0/OntologiBatikTanty#Warna_Alami</uri>
</binding>
</result>
</results>
</sparql>
`
I make foreach looping in php like this: `
$xml = simplexml_load_string($hasil) or die ("error cannot create object");
$results = $xml->results->result;
$jumlah=count($results);
echo "Total hasil pencarian : $jumlah <br><br>";
//$keyword2=array();
$output=array();
foreach($xml->results->result as $nodes){
$jumlah=count($nodes);
foreach ($nodes->binding as $key =>$node) {
$output=$node->uri;
echo $output;
echo "<br><br>";
echo $keyword=substr($output,66);
echo "<br><br>";
}
}
echo"<hr>";
echo $keyword[0];
`
My question is how to acces the data array from outside looping. for example i want to acces index[0]. but the output only "W" it should be "Soga_Jawa"
solved your issue please check this code
$xml = simplexml_load_string($hasil) or die ("error cannot create object");
$results = $xml->results->result;
$jumlah=count($results);
echo "Total hasil pencarian : $jumlah <br><br>";
//$keyword2=array();
$output=array();
$keyword = array();
foreach($xml->results->result as $nodes){
$jumlah=count($nodes);
foreach ($nodes->binding as $key =>$node) {
$output=$node->uri;
echo $output;
echo "<br><br>";
echo $keyword[]=substr($output,66);
echo "<br><br>";
}
}
echo"<hr>";
echo $keyword[0];
use keyword as array type and get it to out of loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.