getting Last Item from XML Node

hi I'm looping thru a xml and all the information output is correct but now i want to get a the the last element value of <reading> not each one.
      <customer id="000">
                  <date dt="29/11/2008">
                        <payDate>new acc</payDate>
                  <date dt="28/02/2009">
                  <date dt="29/05/2009">
                  <date dt="29/08/2009">
                                                                ****<reading>66348</reading>***//want to get this value

$doc = new DomDocument;
				$doc->Load('files/customers.xml') or die('Unable to load file');
				$count = 0;
				$i = 0;
					 $node = $doc->getElementsByTagName('customer');
						foreach( $node as $find ){
							$customerMeters = $find->getElementsByTagName('meter');
								foreach($customerMeters as $customerMeter){
								$meter = $customerMeter->nodeValue;
									if ($mnum == $meter){
										$fnames = $doc->getElementsByTagName('firstName');
										$fname = $fnames->item($count)->nodeValue;
										$lnames = $doc->getElementsByTagName('lastName');
										$lname = $lnames->item($count)->nodeValue;	
										echo "Updating Meter reading and billing for <b>".$fname." ".$lname."</b>";
										// *************************
										$datesNodes = $find->getElementsByTagName('dates');
											foreach ($datesNodes as $datesNode) { 
												$dates = $datesNode->getElementsByTagName('date'); 
													foreach ($dates as $date) { 
														$readings = $date->getElementsByTagName("reading")->item(last)->nodeValue;
														echo "<br />".$readings;
													}// foreach
								}// end foreach meter

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bobby101Author Commented:
oh sorry this is coming from a form as attached
<form name="reading" method="post" action="">
<table cellspacing="3">
	<tr><td align="right">Date:</td><td><input type="text" name="date" size="20" /> DD/MM/YYYY</td></tr>
	<tr><td align="right">Meter Number:</td><td><input type="text" name="mnum" size="20" /></td></tr>
	<tr><td align="right">Meter Reading:</td><td><input type="text" name="readings" size="20" /></td></tr>
	<tr><td>&nbsp;</td><td><input type="submit" value="Add" name="add" /></td></tr>
	<tr><td>&nbsp;</td><td><p><span style="color:red;"><?php echo $error; ?></span></p></td></tr>

Open in new window

When you exit this loop, $reading has the last value

foreach ($dates as $date) {
    $readings = $date->getElementsByTagName("reading")->item(last)->nodeValue;
    //echo "<br />".$readings;
}// foreach
Shinesh PremrajanEngineering ManagerCommented:
I am not sure with the code, its worth giving a try ..

Just get the count of the dates in the xml

$dateCount= count($date->item);


$dateCount= count($date);

$dateCount=$dateCount-1; // as it index starts with 0

then use the date count in place of the last variable  in your code.
like given below

$readings = $date->getElementsByTagName("reading")->item($dateCount)->nodeValue;

Hope this helps.

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

bobby101Author Commented:
$readings = $date->getElementsByTagName("reading")->item(last)->nodeValue;

error message:
Warning: DOMNodeList::item() expects parameter 1 to be long, string given in C:\xampplite\htdocs\dwax\processmeter.php
I see, you can just give it 0

$readings = $date->getElementsByTagName("reading")->item(0)->nodeValue;

Then when the loops end, $readings contains the value you want.
bobby101Author Commented:
item(0) bring the first value
Have you tried this?

foreach ($dates as $date) {
    $readings = $date->getElementsByTagName("reading")->item(0)->nodeValue;
}// foreach
echo "<br />".$readings;

echo the result only after you exit the loop - it will be the last reading

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bobby101Author Commented:
I think you can do it as below:

$readings = $dates[$dates.length - 1]->getElementsByTagName("reading")->item(0)->nodeValue;
echo "<br />".$readings;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Components

From novice to tech pro — start learning today.