php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71673 Cannot access array key with hyphen in it
Submitted: 2016-02-26 19:44 UTC Modified: 2016-02-27 09:06 UTC
From: onskulis at yahoo dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.6.18 OS: Mac,Linux
Private report: No CVE-ID: None
 [2016-02-26 19:44 UTC] 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);
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-26 21:03 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-02-26 21:03 UTC] 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-27 09:00 UTC] 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-27 09:06 UTC] requinix@php.net
Yes it does have nulls. https://3v4l.org/vgfI1

See http://php.net/support.php if you're still having trouble understanding this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 01:01:30 2024 UTC