PHP: Deserializing a XML to an array -


a month ago asked how deserialize xml in c#, have same in php, can't make work correctly, 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 i'm trying do:

$row = new simplexmlelement($xml);      $json = json_encode($row);     $array = json_decode($json,true);       foreach($array["result"]["rows"]["row"] $row){             $array[$k] = [                     'name1' => $row["column"][0],                     'name2' => $row["column"][1]                     ];                $k++;     } 

the problem if use foreach like:

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

gives me 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 want last 2 results prints, because need, how can fix this?

your there. came 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'] $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'] value1

you can start second foreach loop them all

<?php  foreach($array['result']['rows']['row'] $key=>$value){     foreach($value['column'] $k=>$v){         echo "[name".$k++."] = $v<br />";         // or can have in array         $new_array[]["name".$k++]=$v;     } }  ?>  [name1] = value1 [name2] = value2 [name1] = value1 [name2] = value2 

$new_array like

array (     [0] => array         (             [name1] => value1         )      [1] => array         (             [name2] => value2         )      [2] => array         (             [name1] => value1         )      [3] => array         (             [name2] => value2         )  ) 

you can group them changing [] @ end of string this

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

the result be:

array (     [name1] => array         (             [0] => value1             [1] => value1         )      [name2] => array         (             [0] => value2             [1] => value2         )  ) 

instead of using [] can use $key number of first loop , group them differently

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

will output , array this

array (     [0] => array         (             [name1] => value1             [name2] => value2         )      [1] => array         (             [name1] => value1             [name2] => value2         )  ) 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -