Page 1 of 1

how to get the value of the index

Posted: Wed Oct 25, 2006 9:27 pm
by iffo
Hi,

I am using simplexml_load_string which gives me an object with one single dimensional array.


SimpleXMLElement Object ( [title] => Forty What? [from] => Joe [to] => Jane [body] => I know thats the answer -- but whats the question? )



Everything looks good, as I wanted to run the for loop and get the values out of the array ....... code below

Code: Select all

$xmlblock="<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>";

$xmlparsed = simplexml_load_string($xmlblock);   


	foreach ($xmlparsed  as $s  => $v)
	{
		
      		print_r($v);
	   
	}
The result of print_r($v); gives me instead of values following




SimpleXMLElement Object ( [0] => Forty What? ) SimpleXMLElement Object ( [0] => Joe ) SimpleXMLElement Object ( [0] => Jane ) SimpleXMLElement Object ( [0] => I know that's the answer -- but what's the question? )

But i need to retrive only the values like "Joe" "Jan" how could I do that? Thanks

Posted: Wed Oct 25, 2006 9:45 pm
by s.dot

Code: Select all

<?php
$xmlblock="<document> 
 <title>Forty What?</title> 
 <from>Joe</from> 
 <to>Jane</to> 
 <body> 
  I know that's the answer -- but what's the question? 
 </body> 
</document>"; 

$xmlparsed = simplexml_load_string($xmlblock);    
foreach($xmlparsed AS $k => $v)
{
	echo $v;
}
?>

Posted: Wed Oct 25, 2006 10:42 pm
by Cameri

Code: Select all

<?php
$xmlblock="<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>";

$xmlparsed = simplexml_load_string($xmlblock);   
foreach($xmlparsed AS $k => $v)
{
        echo $k." => "; //key
        echo $v[0]; //value
}
?>

Posted: Thu Oct 26, 2006 3:06 am
by volka
Usually you have more than one entry in your document.

Code: Select all

$xmlblock="<document>
	<entry>
		<title>Forty What?</title>
		<from>Joe</from>
		<to>Jane</to>
		<body>I know that's the answer -- but what's the question?</body>
	</entry>
	<entry>
		<title>2001</title>
		<from>HAL9000</from>
		<to>Dave</to>
		<body>Will I dream?</body>
	</entry>
</document>";

$doc = simplexml_load_string($xmlblock);   


foreach ($doc->entry  as $v)
{
	echo "<tr>
			<td>{$v->title}</td>
			<td>{$v->from}</td>
			<td>{$v->to}</td>
			<td>{$v->body}</td>
		</tr>";
}

Posted: Thu Oct 26, 2006 8:13 am
by iffo
thanks a lot to everybody ........ it works