How can I use a php array as a path to target a value in another array?

后端 未结 3 1505
你的背包
你的背包 2020-12-21 03:49

I want to access a specific array\'s property by using a separate array as the path. The problem is the property in question may be at any depth. Here\'s an example...

3条回答
  •  囚心锁ツ
    2020-12-21 04:12

    A simple loop should work:

    Update: sorry did check my code

    foreach($path as $id) 
    {
        $data = $data[$id];
    }
    
    echo $data;
    

    Result:

    deeper_value
    

    This will overwrite the $data array so you might want to make a copy of $data first like David did in his example.

提交回复
热议问题