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

后端 未结 3 1507
你的背包
你的背包 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:13

    It's not the greatest code, but should work:

    function getValue($pathArray, $data)
    {
       $p = $data;
       foreach ($pathArray as $i)
       {
         $p = $p[$i];
       }
    
       return $p;
    }
    

提交回复
热议问题