0

A month ago I asked how to deserialize a XML in c#, now I have to do the same but in PHP, but I can't make it work correctly, This is my XML format:

<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/api/" action="EXPORT">
<result>
    <rows>
        <row>
            <column name="Name1">Value1</column>
            <column name="Name2">Value2</column>
        </row>
        <row>
            <column name="Name1">Value1</column>
            <column name="Name2">Value2</column>
        </row>
    </rows>
</result>
</response>

And here is what I'm trying to do:

$row = new SimpleXMLElement($XML);

    $json = json_encode($row);
    $array = json_decode($json,TRUE);


    foreach($array["result"]["rows"]["row"] as $row){

           $array[$k] = [
                    'Name1' => $row["column"][0],
                    'Name2' => $row["column"][1]
                    ];

              $k++;
    }

The problem with this is that if a use a foreach like:

foreach($array as $arr){
            print_r($arr);
            echo"<br>";
}

Gives me this result:

Array ( [uri] => /api/ [action] => EXPORT )

Array ( [rows] => Array ( [row] => Array ( [0] => Array ( [column] => Array ( [0] => Value1 [1] => Value2 ) ) [1] => Array ( [column] => Array ( [0] => Value1 [1] => Value2 ) ) ) ) )

Array ( [Name1] => Value1 [Name2] => Value2 )

Array ( [Name1] => Value1 [Name2] => Value2 )

And I just want the last 2 results it prints, because that was actually what I need, how can I fix this?

elunap
  • 841
  • 3
  • 11
  • 30
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – ThW Jul 28 '17 at 11:30

1 Answers1

0

Your almost there. This is what i came up with

<?php

$XML='<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/api/" action="EXPORT">
<result>
    <rows>
        <row>
            <column name="Name1">Value1</column>
            <column name="Name2">Value2</column>
        </row>
        <row>
            <column name="Name1">Value1</column>
            <column name="Name2">Value2</column>
        </row>
    </rows>
</result>
</response>';

$row = new SimpleXMLElement($XML);

$json = json_encode($row);
$array = json_decode($json,TRUE);

foreach($array['result']['rows']['row'] as $key=>$value){
    echo '<pre>';
    print_r($value);
    echo '</pre>';
}

?>

Array
(
    [column] => Array
        (
            [0] => Value1
            [1] => Value2
        )

)

Array
(
    [column] => Array
        (
            [0] => Value1
            [1] => Value2
        )

)

You can use $value['column']['0'] to get Value1

You can also start a second foreach to loop them all

<?php

foreach($array['result']['rows']['row'] as $key=>$value){
    foreach($value['column'] as $k=>$v){
        echo "[Name".$k++."] = $v<br />";
        // or you can have it in an array like this
        $new_array[]["Name".$k++]=$v;
    }
}

?>

[Name1] = Value1
[Name2] = Value2
[Name1] = Value1
[Name2] = Value2

$new_array would then look like

Array
(
    [0] => Array
        (
            [Name1] => Value1
        )

    [1] => Array
        (
            [Name2] => Value2
        )

    [2] => Array
        (
            [Name1] => Value1
        )

    [3] => Array
        (
            [Name2] => Value2
        )

)

You can also group them together by changing the [] at the end of the string like this

$new_array["Name".$k++][]=$v;

The result would then be:

Array
(
    [Name1] => Array
        (
            [0] => Value1
            [1] => Value1
        )

    [Name2] => Array
        (
            [0] => Value2
            [1] => Value2
        )

)

Instead of using [] you can use the $key number of the first loop and that will group them differently

$new_array[$key]["Name".$k++]=$v;

Will output and array like this

Array
(
    [0] => Array
        (
            [Name1] => Value1
            [Name2] => Value2
        )

    [1] => Array
        (
            [Name1] => Value1
            [Name2] => Value2
        )

)
Patrick Simard
  • 2,294
  • 3
  • 24
  • 38