#35621 [NEW]: casting to array is not working as expected

From: Date: Sat, 10 Dec 2005 08:26:05 +0000
Subject: #35621 [NEW]: casting to array is not working as expected
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-90193@lists.php.net to get a copy of this message
From:             phpdude at fredesign dot com
Operating system: Macintosh OSX (1.3)
PHP version:      5.1.1
PHP Bug Type:     SimpleXML related
Bug description:  casting to array is not working as expected

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 ) ) 

-- 
Edit bug report at http://bugs.php.net/?id=35621&edit=1
-- 
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=35621&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): http://bugs.php.net/fix.php?id=35621&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=35621&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=35621&r=fixedcvs
Fixed in release:             http://bugs.php.net/fix.php?id=35621&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=35621&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=35621&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=35621&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=35621&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=35621&r=notwrong
Not enough info:              http://bugs.php.net/fix.php?id=35621&r=notenoughinfo
Submitted twice:              http://bugs.php.net/fix.php?id=35621&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=35621&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=35621&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=35621&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=35621&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=35621&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=35621&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=35621&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=35621&r=mysqlcfg


Thread (2 messages)

« previous php.bugs (#90193) next »