PHP动态地从子数组

时间:2015-07-14 12:07:26

标签: php arrays

我有一个问题。 有一个重命名数组键的脚本。

现在我想在需要时在子级别数组中执行此操作。

现状

我现有的代码是:

    ///######## IF THE ORIGINAL KEY HAS BEEN SET
    if(isset($this->PreparedData[$key]) === true){
        ///######## ADD THE DATA TO THE PREPARED DATA VARIABLE
        $this->PreparedData[$value] = $this->PreparedData[$key];
        ///######## UNSET THE ORIGINAL KEY
        unset($this->PreparedData[$key]);
    }

但我希望代码能够动态设置以下内容:

    ///######## IF THE ORIGINAL KEY HAS BEEN SET
    if(isset($this->PreparedData[$subKey1][$key]) === true){
        ///######## ADD THE DATA TO THE PREPARED DATA VARIABLE
        $this->PreparedData[$subKey1][$value] =$this->PreparedData[$subKey1][$key];
        ///######## UNSET THE ORIGINAL KEY
        unset($this->PreparedData[$subKey1][$key]);
    }

问题

但我想动态地这样做:

所以它可能是:

$this->PreparedData[$subKey1][$key]

但是:

$this->PreparedData[$subKey1][$subKey2][$key]

但是:

$this->PreparedData[$subKey1][$subKey2][$subKey3][$key]

这是在数组的手中

期望的情况

所以我可以设置:

MethodName('wheels', array('car', 'mustang'));

这意味着:

$this->PreparedData['car']['mustang']['wheels']

我唯一的问题..是......怎么办呢? 因为我也希望能够致电:

MethodName('wheels');

含义:

$this->PreparedData['wheels']

谢谢!!

解决方案是(通过splash58):

function MethodName($key, $path = array()) {
   global $array;

   $p = &$array;                 // point to array
   foreach($path as $step)       // walk trough path to needed level
      $p = &$p[$step];
   return  $p[$key];             //take value 
}

$array = array(                  // test array
  'wheels'=> 'wheels1', 
  'car' => array(
      'mustang'  => array(
          'wheels'=> 'wheels2')));

echo MethodName('wheels', array('car', 'mustang')) . "\n";    // wheels2
echo MethodName('wheels');                                    // wheels1

3 个答案:

答案 0 :(得分:1)

将此移至您的环境

function MethodName($key, $path = array()) {
   global $array;

   $p = &$array;                 // point to array
   foreach($path as $step)       // walk trough path to needed level
      $p = &$p[$step];
   return  $p[$key];             //take value 
}

$array = array(                  // test array
  'wheels'=> 'wheels1', 
  'car' => array(
      'mustang'  => array(
          'wheels'=> 'wheels2')));

echo MethodName('wheels', array('car', 'mustang')) . "\n";    // wheels2
echo MethodName('wheels');                                    // wheels1

答案 1 :(得分:0)

声明方法时,只需让它具有数组参数的默认值即可。做这样的事情:

function MethodName($valToInsert, $array = array())
{
    $arr1 = array();
    foreach($array as $key=>$value)
    {
        $arr1 = arr1[$value];
    }
    $final_array = $arr1[$valToInsert];
    return $final_array;
}

答案 2 :(得分:0)

对于这个问题,我使用以下函数:

function traverse($array, $path){
    foreach ($path as $key) {
        if(isset($array[$key]))
            $array = &$array[$key];
        else return null;
    }
    return $array;
}

使用它非常简单:

$array = array(                  // test array
    'wheels'=> 'wheels1', 
    'car'   => array(
        'mustang'  => array(
            'wheels'=> 'wheels2')
        )
);

traverse($array, array('car', 'mustang', 'wheels')); // will return 'wheels' if path exists 'null' if not

我个人这样使用它:

$array = array(                  // test array
    'wheels'=> 'wheels1', 
    'car'   => array(
        'mustang'  => array(
            'wheels'=> 'wheels2')
        )
);

traverse($array, 'car.mustang.wheels');

function traverse($array, $path){
    if(!is_array($path))
        $path = explode(".", $path);
    foreach ($path as $key) {
        if(isset($array[$key]))
            $array = &$array[$key];
        else return null;
    }
    return $array;
}