Bug #71673 [Nab]: Cannot access array key with hyphen in it

From: Date: Sat, 27 Feb 2016 09:06:16 +0000
Subject: Bug #71673 [Nab]: Cannot access array key with hyphen in it
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199499@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71673&edit=1 ID: 71673 Updated by: requinix@php.net Reported by: onskulis at yahoo dot de Summary: Cannot access array key with hyphen in it Status: Not a bug Type: Bug Package: Arrays related Operating System: Mac,Linux PHP Version: 5.6.18 Block user comment: N Private report: N New Comment: Yes it does have nulls. https://3v4l.org/vgfI1 See http://php.net/support.php if you're still having trouble understanding this. Previous Comments: ------------------------------------------------------------------------ [2016-02-27 09:00:44] onskulis at yahoo dot de But at at the point where I am trying to access an element of $test array, the argument $param of getPost function is well-formated, string without any nulls. Why still I cant use that argument as array key ? It contains no 0, no nulls. ------------------------------------------------------------------------ [2016-02-26 21:03:05] requinix@php.net The array keys are not simply class+property. They contain null characters. It's how PHP manages private class properties, and casting to an object will expose those nulls to you. https://3v4l.org/au0ON Create a method inside the class that returns the result of get_object_vars(). http://php.net/get-object-vars ------------------------------------------------------------------------ [2016-02-26 19:44:28] onskulis at yahoo dot de Description: ------------ Cannot access array key with hyphen in it if passing a variable as key, but it works with string. Test script: --------------- <?php $test = array('customer-test1' => 1, 'customer-test2' => 2); function getPost($param) { global $test; echo $param." = ".$test[$param]."<br/>"; return isset($test[$param]) ? $test[$param] : NULL; } class Customer { private $test1; private $test2; function __construct() { } } $object = new Customer(); $arr = (array)$object; $newArr = array(); foreach($arr as $key => $val) { $newKey = str_replace(get_class($object), "", $key); $newArr[$newKey] = getPost(strtolower(get_class($object))."-".$newKey); } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71673&edit=1

« previous php.bugs (#199499) next »