0

I have a XML which I converted to array without a problem, the original XML looks something like this.

<somedata>data</somedata>
<somedata2>data2</somedata2>
<level1>
   <listings>
     <element>
        <title>AAA</title>
        <description>DescriptionA</description>
        <moredata>
          <adate>2012/12/12</adate>
        </moredata>
     </element>

     <element>
        <title>AAA</title>
        <description>DescriptionA</description>
        <moredata>
          <adate>2013/10/12</adate>
        </moredata>
     </element>

     <element>
        <title>BBB</title>
        <description>DescriptionB</description>
        <moredata>
          <adate>2012/09/01</adate>
        </moredata>
     </element>
   </listings>
<level1>

Of course so far everything looks fine, I get my nice array with all the child objects, but I need to create an HTML output by grouping the elements by for example the Title, considering that my Output would look something like this.

Title: AAA
Description: DescriptionA
Date 1: 2012/12/12
Date 2: 2013/10/12

Title: BBB
Description: DescriptionB
Date 1: 2012/09/01

Notice I have grouped AAA which was repeated and listed both dates within the same result.

How in PHP, I was seriously thinking on saving to mysql then pulling them back, but that will simply be a bad patch.

Hope anyone can help.

Regards

1
  • Are you able to restructure your source XML document? that would be more logical and save bytes. Commented May 1, 2012 at 2:30

1 Answer 1

2

this is a rough concept how i'd do it.

you have to parse the xml with simplexml into an array, after that you can customize your html output.

$result = array();

foreach($xml->element as $element){

   $line = array();
   $line['date'] = $element->date;
   $line['description'] = $element->description;

   $result[$element->title]['line'][] = $line;

}
Sign up to request clarification or add additional context in comments.

4 Comments

I kind of understand your code, perhaps I cant implement it, need something more precise for the depth and example above, the sorting factor is the name in this case. 4 to 5 levels. THANKS!!!!!
I already have an array out of the XML, I am stuck on the process of grouping each LISTING on a unique list with the dates inside combines as the example above.
do you want to walk through $xml->somedata->level1. be more specific
somedata we dont need it, everything starts from "listings", for each "element" found, create the array, but those repeated I want to create an array of dates, instead of having the same "element" twise, only once but with an array of dates which came from all the listings found with the same name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.