php xmlstring passsing to function behaves unexpected

ehya

New Member
What's the difference between function 1 and function 2. Why function 1 and function 2 are printing different result?\[code\]<?php function toArray( $xmlstring ) { $array = $xmlstring; foreach ( array_slice($array, 0) as $key => $value ) { if ( empty($value) ) $array[$key] = NULL; elseif ( is_array($value) ) $array[$key] = toArray($value); } return $array; } function toArray1( $xmlstring ) { $xml = simplexml_load_string($xmlstring); $json = json_encode($xml); $array = json_decode($json,TRUE); $array = $xmlstring; foreach ( array_slice($array, 0) as $key => $value ) { if ( empty($value) ) $array[$key] = NULL; elseif ( is_array($value) ) $array[$key] = toArray($value); } return $array; } $strString1 = <<<KOOL<?xml version="1.0"?><result type="ok"><books><book>English</book><book>Nepali</book></books></result>KOOL;$strString2 = <<<KOOL<?xml version="1.0"?><result type="ok"><books><book>Math</book></books></result>KOOL; $xml = simplexml_load_string($strString1); $json = json_encode($xml); $array = json_decode($json,TRUE); echo 'Before Array 1: '; var_dump( toArray( $array ) ); echo 'Before Array 2: '; $xml = simplexml_load_string($strString2); $json = json_encode($xml); $array = json_decode($json,TRUE); var_dump( toArray( $array ) ); echo '<br/>Before 3 [Method 2]:'; var_dump( toArray1( $strString1 ) ); echo '<br/>Before 4 [Method 2]:'; var_dump( toArray1( $strString2 ) );?>\[/code\]
 
Back
Top