开发者

Storing a multidimensional array as an object variable, adding additional keys to the array

So I have a 开发者_StackOverflowmultidimensional array which is stored in an object. I want to add additional keys to this array.

Here's what I have:

$object->pathsArray = array(
    "key1" => array('path' => '/some/path/to/some/file.php', 'action' => 'index'),
    "key2" => array('path' => '/some/path/to/some/class.php', 'action' => 'method2')
);

And here's what I assumed would work but did not:

$object->pathsArray['key3'] = array('path' => '/some/path/to/some/method/or/script.php', 'action' => 'method3');

My first workaround:

$newPathsArray = array("key3" => array('path' => '/some/path/to/some/method/or/script.php', 'action' => 'method3'));   
$object->pathsArray = array_merge($object->pathsArray, $newPathsArray);

Another workaround that SHOULD work:

$tempPathsArray = $object->pathsArray;
$tempPathsArray['key3'] = array('path' => '/some/path/to/some/method/or/script.php', 'action' => 'method3');
$object->pathsArray = $tempPathsArray;

So my question: Is there a simpler syntax (ie: one line solution) or am I forced to bring in a temp. variable, append to that then merge/re-assign the value to the object?


Sorry to write an answer, but I'm not able to comment. I think that it's not correct to make an attribute public just to use it by that way. The correct thing should be make a setter to fill it, not modifying the class design just for that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜