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

From: Date: Sat, 27 Feb 2016 09:00:53 +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-199498@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
 User updated by:    onskulis at yahoo dot de
 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:

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.


Previous Comments:
------------------------------------------------------------------------
[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


Thread (4 messages)

« previous php.bugs (#199498) next »