1

I first had an XML file which I converted to the array that can be seen below. But the problem I am having is looping through the array and calling values. For instance, how would I loop through anime to get the anime name 'attack on titan' or start time, '9.30am' etc? How can I do this? Thank you.

Here's the array:

Array (
 [anime] => Array (
  [0] => Array (
   [id] => 1
    [content] => Array (
     [action_adventure_channel] => Array (
      [0] => Array (
       [name] => Array (
        [0] => Attach on Titan
        )
        [start_time] => Array (
         [0] => 09.30am
        )
        [end_time] => Array (
         [0] => 10.30am
        )
       )
      )
     )
    )
    [1] => Array (
     [id] => 2
     [content] => Array (
      [crime_drama_channel] => Array (
       [0] => Array (
        [name] => Array (
         [0] => Death Note
        )
        [start_time] => Array (
         [0] => 12.00pm
        )
        [end_time] => Array (
         [0] => 1.00pm
        )
       )
      )
     )
    )
   )
  )

EDIT: Here is the XML version that I converted to the above array:

<?xml version="1.0" encoding="UTF-8"?>
<anime_data>
    <anime id="1">
    <action_adventure_channel>
        <name>Attach on Titan</name>
        <start_time>09.30am</start_time>
        <end_time>10.30am</end_time>
    </action_adventure_channel>
    </anime>
    <anime id="2">
    <crime_drama_channel>
        <name>Death Note</name>
        <start_time>12.00pm</start_time>
        <end_time>1.00pm</end_time>
    </crime_drama_channel>
    </anime>
</anime_data>
5
  • Is this data fetched from somewhere or can you alter the format of the array? Because I find it weird the name attributes is an array as there should only be one name for each element. Commented Jan 18, 2015 at 11:20
  • Um would have been much easier xml... Commented Jan 18, 2015 at 11:21
  • or you could just use SimpleXML and straight up parsed those values. Commented Jan 18, 2015 at 11:22
  • this array does not a valid php array ! Commented Jan 18, 2015 at 11:24
  • I've posted the xml version above. I too thought it looked wrong and was trying to change it to make it look more like an array but it wouldn't work. How could I convert it to a proper array then? or use SimpleXML to retrieve values? Please? Commented Jan 18, 2015 at 11:28

1 Answer 1

1

You could just use SimpleXML from the start. Then just traverse using foreach.

$xml = simplexml_load_file('path/to/xmlfile.xml');
foreach($xml as $anime) {
    $id = $anime->attributes()->id;
    foreach($anime as $info) {
        $name = $info->name;
        $start_time = $info->start_time;
        $end_time = $info->end_time;
    }
}

Sample Output

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

Comments

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.