Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:29:46+00:00 2026-06-14T19:29:46+00:00

I am trying to loop though some XML and preform an action if an

  • 0

I am trying to loop though some XML and preform an action if an attribute value did or didn’t match the value it had on the previous iteration of the loop.

The incoming data is already sorted by the class.

Here is some sample XML and code

<character_list>
    <character id="00001">
        <name first="Name1"/>
        <type class="Class A" classid="1"/>
    </character>
    <character id="00002">
        <name first="Name2"/>
        <type class="Class A" classid="1"/>
    </character>
    <character id="00003">
        <name first="Name3"/>
        <type class="Class B" classid="2"/>
    </character>
    <character id="00004">
        <name first="Name4"/>
        <type class="Class B" classid="2"/>
    </character>
    <character id="00005">
        <name first="Name5"/>
        <type class="Class B" classid="2"/>
    </character>
    <character id="00006">
        <name first="Name6"/>
        <type class="Class C" classid="3"/>
    </character>
    <character id="00007">
        <name first="Name7"/>
        <type class="Class D" classid="4"/>
    </character>
    <character id="00008">
        <name first="Name8"/>
        <type class="Class D" classid="4"/>
    </character>
</character_list>

Here is my code

<?php
$query = "data.xml";
$xml = file_get_contents($query);

try {
        $dataobj = NEW SimpleXMLElement($xml);
}

catch(Exception $e) {
        echo "Parsing error!  ".$e->getMessage()." in ".basename($e->getFile()).":".$e->getLine()."!";
        exit;
}

$lastclass = 0;

foreach ($dataobj as $xmldata) {

        $currentclass = $xmldata->type['classid'];

        if ($lastclass == $currentclass) {
                echo $xmldata->name['first']." ";
        }
        else
        {
                echo "<br>";
                echo $xmldata->type['class']." - ";
                echo $xmldata->name['first']." ";
        }

        $lastclass = $xmldata->type['classid'];
}


?>

Output from the code

Class A - Name1 
Class A - Name2 
Class B - Name3 
Class B - Name4 
Class B - Name5 
Class C - Name6 
Class D - Name7 
Class D - Name8

Expected output

Class A - Name1 Name2 
Class B - Name3 Name4 Name5 
Class C - Name6 
Class D - Name7 Name8

Can anyone see what I am doing wrong and how to correct it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:29:47+00:00Added an answer on June 14, 2026 at 7:29 pm

    Just cast values to strings like that:

    foreach ($dataobj as $xmldata) {
    
        $currentclass = (string) $xmldata->type['classid'];
    
        if ($lastclass == $currentclass) {
                echo $xmldata->name['first']." ";
        }
        else
        {
                echo "<br>";
                echo $xmldata->type['class']." - ";
                echo $xmldata->name['first']." ";
        }
    
        $lastclass = (string) $xmldata->type['classid'];
    }
    

    Without casting you are comparing simplexml objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through some XML and set the value of a
Just trying to get some rows out of a database and loop through but
I am trying to loop though my users database to show each username in
I'm confused. I'm trying to loop though 2 files looking at the first token
I am trying to loop through some controls in a Powershell / WPF application.
I am trying to convert some xml into a json object using PHP. This
I've got a xml that I'm parsing and trying to extract some data from.
I'm trying to loop through a list of results from MySQL and display all
I'm trying to loop through objects from my database in the template and one
I'm trying to loop through all files of .properties extension, in a root folder:C:\ExecutionSDKTest_10.2.2\,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.