php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31072 var_export() does not output an array element with an empty string key.
Submitted: 2004-12-13 15:55 UTC Modified: 2004-12-17 15:40 UTC
From: komura at ma9 dot seikyou dot ne dot jp Assigned: helly (profile)
Status: Closed Package: Variables related
PHP Version: 4.3.10RC2 OS: Linux
Private report: No CVE-ID: None
 [2004-12-13 15:55 UTC] komura at ma9 dot seikyou dot ne dot jp
Description:
------------
var_export() in PHP 4.3.10RC1/RC2 does not output an array element with an empty string key.

It appears that the following patch is the cause of this problem.

http://cvs.php.net/diff.php/php-src/ext/standard/var.c?r1=1.150.2.14&r2=1.150.2.15&ty=u

@@ -260,13 +267,17 @@ static int php_array_element_export(zval
 	if (hash_key->nKeyLength==0) { /* numeric key */
 		php_printf("%*c%ld => ", level + 1, ' ', hash_key->h);
 	} else { /* string key */
-		char *key;
-		int key_len;
-		key = php_addcslashes(hash_key->arKey, hash_key->nKeyLength - 1, &key_len, 0, "'\\", 2 TSRMLS_CC);
-		php_printf("%*c'", level + 1, ' ');
-		PHPWRITE(key, key_len);
-		php_printf("' => ");
-		efree(key);
+		if (va_arg(args, int) && hash_key->arKey[0] == '\0') {
+			return 0;
+		} else {
+			char *key;
+			int key_len;
+			key = php_addcslashes(hash_key->arKey, hash_key->nKeyLength - 1, &key_len, 0, "'\\", 2 TSRMLS_CC);
+			php_printf("%*c'", level + 1, ' ');
+			PHPWRITE(key, key_len);
+			php_printf("' => ");
+			efree(key);
+		}
 	}

Reproduce code:
---------------
var_export( array( '' => 1, 'a' => 2 ) );

Expected result:
----------------
array (
  '' => 1,
  'a' => 2,
)


Actual result:
--------------
array (
  'a' => 2,
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-13 16:34 UTC] derick@php.net
Marcus, you broke this... so fix it :)
 [2004-12-17 15:40 UTC] derick@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC