Unexpected results when converting a namespaced object to an array

I'm having unexpected results when converting a namespaced object to an array.

<?php

namespace package\test;

class Test {
    private $foo;
    private $bar;

}

$test = new Test();
$testArray = (array) $test;

var_dump($testArray);

and the output is

array
    '�package\test\Test�foo' => null
    '�package\test\Test�bar' => null

Not sure what those � characters are from the var_dump? I looked in the source and it appears to be &#0;. Basically what I need to do is trim the keys so it ends up being

array
    'foo' => null
    'bar' => null

but I'm not sure how to target those � characters with a regular expression to get rid of the part that I don't want? This is for PHP 5.3.3. Thanks.

Answers


You actually don't need any regular expressions. You can do the following:

foreach ($testArray as $key => $value) {
    $pos = strrpos($key, chr(0)); // find the last null character
    if ($pos !== false) {
        $testArray[substr($key, $pos+1)] = $value;
        unset($testArray[$key]);
    }
}

Every character that cannot be displayed will be replaced with a black diamond (U+FFFD REPLACEMENT CHARACTER) by the browser. So in this case it wouldn't even make sense to check for this particular substitute character.


Need Your Help

Adding a row to mySQL using a session and a form with PDO

php mysql forms session pdo

I am trying to add a new row to a table in my database, there is only two columns in the table and i am using a form to get one of the fields. here is the form i am using

Problems Trying to Find User Location in MKMapView

ios mkmapview cllocationmanager

I am having problems trying to get the user's current position to display on an MKMapView. Here is the relevant code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.