John Davidson

simplexml - Get next value in PHP

0 comments
Message:


In an XML file i have some attributes


<logoSize logoSizeCm2="6">
<logoSize logoSizeCm2="24">
<logoSize logoSizeCm2="60">
<logoSize logoSizeCm2="120">
<logoSize logoSizeCm2="240">

What i want is to get every logoSizeCm2 value that is lower than $prpurlNumber plus the next one.


foreach ($XMLB->logoSize as $prodar) {
$prodb = $prodar[0];
$pripurl = $prodb->xpath("@logoSizeCm2");
$prpurlNumber = '65';
if ($pripurl <= $prpurlNumber) {
echo $prpurl;
}
}

This outputs 6 24 60 but what i want is to also get the next one like this 6 24 60 120
Any help would be much appreciated.



My Answer:
To get the next value in an XML file using SimpleXML in PHP, you can use the `next()` function to move the pointer to the next sibling node. Here is an example:

php
$xml = simplexml_load_file('your_xml_file.xml');

// Loop through each node
foreach ($xml->children() as $node) {
// Get the value of the current node
$value = (string) $node;

// Move the pointer to the next sibling node
$nextNode = $node->next();

// Get the value of the next node
$nextValue = (string) $nextNode;

// Output the values
echo "Current Value: $value
";
echo "Next Value: $nextValue
";
}


This code will iterate through each node in the XML file, get the value of the current node, move the pointer to the next sibling node, and get the value of the next node. You can then use these values as needed in your application.

Rate this post

3 of 5 based on 6779 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us