how to get the value of the index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

how to get the value of the index

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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;
}
?>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post 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
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>";
}
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post by iffo »

thanks a lot to everybody ........ it works
Post Reply