Home > Net >  PHP - How to read exact information from XML string?
PHP - How to read exact information from XML string?

Time:02-01

I have a XML string as follows:

$xml_string = "<month total_days='31' number='12' name='December'>
    <day number='1' weekday_number='2' name='Wednesday' active='0'>
    </day>
    <day number='2' weekday_number='3' name='Thursday' active='0'>
    </day>
    ...
    ...
    ...
    <day number='13' weekday_number='0' name='Monday' active='0'>
        <worked>1</worked>
        <active_hours>7</active_hours>
        <inactive_hours>7</inactive_hours>
    </day>
    <day number='14' weekday_number='0' name='Tuesday' active='0'>
        <worked>1</worked>
        <active_hours>12</active_hours>
        <inactive_hours>13</inactive_hours>
    </day>
    ...
    ...
    ...
    <day number='30' weekday_number='3' name='Thursday' active='0'>
    </day>
    <day number='31' weekday_number='4' name='Friday' active='0'>
    </day>
</month>";

One problem I saw is some day attribute contains worked, active_hours, inactive_hours data, and day doesn't.

I tried to read the string and convert it as a PHP array by the following code:

$arr = (array) simplexml_load_string($xml_string);
$arr = json_decode(json_encode($arr), true); //also write this line to test

My target is to read the number from the day, worked, active_hours, and inactive_hours data. If the below is my content:

<day number='13' weekday_number='0' name='Monday' active='0'>
    <worked>1</worked>
    <active_hours>7</active_hours>
    <inactive_hours>7</inactive_hours>
</day>

Then I will get the day is 13, worked is 1, active_hours is 7, inactive_hours is 7. I didn't work with XML before, so I am a little bit confused about how to convert that XML string into PHP array or JSON so that I can run a loop and read the required data. Can anyone help?

  • Thanks

CodePudding user response:

There is no need to convert your xml_string to an array as we can iterate over the xml as is with simple_xml.

We are just looping over each day from your xml and casting the value to an integer and adding that to an array for use later.

I have keyed the array with the day number for convenience and have defaulted the values to zero if they don't exist.

<?php

$xml_string = "<month total_days='31' number='12' name='December'>
    <day number='1' weekday_number='2' name='Wednesday' active='0'>
    </day>
    <day number='2' weekday_number='3' name='Thursday' active='0'>
    </day>
    <day number='13' weekday_number='0' name='Monday' active='0'>
        <worked>1</worked>
        <active_hours>7</active_hours>
        <inactive_hours>7</inactive_hours>
    </day>
    <day number='14' weekday_number='0' name='Tuesday' active='0'>
        <worked>1</worked>
        <active_hours>12</active_hours>
        <inactive_hours>13</inactive_hours>
    </day>
    <day number='30' weekday_number='3' name='Thursday' active='0'>
    </day>
    <day number='31' weekday_number='4' name='Friday' active='0'>
    </day>
</month>";

$arr = simplexml_load_string($xml_string);

foreach($arr->day as $day){
    $key = (int) $day['number'];
    $result[$key]['worked'] = (int) $day->worked ?: 0;
    $result[$key]['active_hours'] = (int) $day->active_hours ?: 0;
    $result[$key]['inactive_hours'] = (int) $day->inactive_hours ?: 0;
}

echo '<pre>';
print_r($result);
echo '</pre>';

Result:

Array
(
    [1] => Array
        (
            [worked] => 0
            [active_hours] => 0
            [inactive_hours] => 0
        )

    [2] => Array
        (
            [worked] => 0
            [active_hours] => 0
            [inactive_hours] => 0
        )

    [13] => Array
        (
            [worked] => 1
            [active_hours] => 7
            [inactive_hours] => 7
        )

    [14] => Array
        (
            [worked] => 1
            [active_hours] => 12
            [inactive_hours] => 13
        )

    [30] => Array
        (
            [worked] => 0
            [active_hours] => 0
            [inactive_hours] => 0
        )

    [31] => Array
        (
            [worked] => 0
            [active_hours] => 0
            [inactive_hours] => 0
        )

)
  •  Tags:  
  • Related