小编Cha*_*les的帖子

如何使用字符串作为数组索引路径来检索值?

假设我有一个类似的数组:

Array
(
    [0] => Array
        (
            [Data] => Array
                (
                    [id] => 1
                    [title] => Manager
                    [name] => John Smith
                )
         )
    [1] => Array
        (
            [Data] => Array
                 (
                     [id] => 1
                     [title] => Clerk
                     [name] =>
                         (
                             [first] => Jane
                             [last] => Smith
                         )
                 )

        )

)
Run Code Online (Sandbox Code Playgroud)

我希望能够构建一个函数,我可以传递一个字符串,作为数组索引路径,并返回适当的数组值而不使用eval().那可能吗?

function($indexPath, $arrayToAccess)
{
    // $indexPath would be something like [0]['Data']['name'] which would return 
    // "Manager" or it could be [1]['Data']['name']['first'] which would return 
    // "Jane" but the …
Run Code Online (Sandbox Code Playgroud)

php arrays

9
推荐指数
2
解决办法
1万
查看次数

标签 统计

arrays ×1

php ×1