php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35621 casting to array is not working as expected
Submitted: 2005-12-10 09:26 UTC Modified: 2005-12-10 09:34 UTC
From: phpdude at fredesign dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.1.1 OS: Macintosh OSX (1.3)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: phpdude at fredesign dot com
New email:
PHP Version: OS:

 

 [2005-12-10 09:26 UTC] phpdude at fredesign dot com
Description:
------------
Hello, when casting a SimpleXMLObject to an array, if the object is empty it remains a SimpleXmlObject, and if the object contains only one value it is converted to a string.

(array) <value /> = simpleXmlObject()
(array) <value>1</value> = "1"
(array) <value>1</value><value>2</value> = array("1","2")

Reproduce code:
---------------
<?php

$s[] = "<root><foo><value /></foo></root>";
$s[] = "<root><foo><value>1</value></foo></root>";
$s[] = "<root><foo><value>1</value><value>2</value></foo></root>";

foreach($s as $sKey=>$xml_string) {
	$o = simplexml_load_string($xml_string);
	$a = (array)$o->foo;
	echo $sKey." = "; print_r($a); echo "\n\n<br /><br />";
}
?>

Expected result:
----------------
0 = Array ( [value] => Array ( ) )

1 = Array ( [value] => Array ( [0] => 1 ) )

2 = Array ( [value] => Array ( [0] => 1 [1] => 2 ) ) 

Actual result:
--------------
0 = Array ( [value] => SimpleXMLElement Object ( ) )

1 = Array ( [value] => 1 )

2 = Array ( [value] => Array ( [0] => 1 [1] => 2 ) ) 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-10 09:34 UTC] phpdude at fredesign dot com
It is casting the external element and not the internal object workings... so this is not a bug report... sorry.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 02:01:29 2024 UTC