简体   繁体   English

PHP动态地从子数组获取数据

[英]PHP dynamically get the data from a sub array

I have a question. 我有个问题。 Have a script that renames array keys. 有一个重命名数组键的脚本。

Now I want to do this in a sub level array if needed. 现在,如果需要,我想在子级数组中执行此操作。

Current situation 现在的情况

The current code I have that works is : 我目前可以正常工作的代码是:

    ///######## 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]);
    }

But I want the code to be able to set the following dynamically: 但我希望代码能够动态设置以下内容:

    ///######## 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]);
    }

Question

But I want to do this dynamically: 但我想动态地做到这一点:

So it could be: 因此可能是:

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

But also: 但是也:

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

But also: 但是也:

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

And this at the hand of an array 而这在数组的手

Desired situation 期望的情况

So I could set: 所以我可以设置:

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

That would mean : 那将意味着:

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

The only question I have.. Is... how to do just this? 我唯一的问题是.....如何做到这一点? Because I also want to be able to call: 因为我也想打电话:

MethodName('wheels');

Meaning: 含义:

$this->PreparedData['wheels']

Thank you!! 谢谢!!

The solution is (by splash58): 解决方法是(通过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

Move this into your enviroment 将此转移到您的环境中

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

When you declare the method, just let it have a default value for the array parameter. 声明该方法时,只需让它具有array参数的默认值即可。 Do something like this: 做这样的事情:

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

For this problem I use the following function: 对于此问题,我使用以下功能:

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

Using it is very simple: 使用它非常简单:

$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

I personally use it like this: 我个人这样使用它:

$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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM