0

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"

1

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.