Alex
Alex

Reputation: 1309

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):

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

Upvotes: 1

Views: 263

Answers (3)

Dănuţ Avădănei
Dănuţ Avădănei

Reputation: 46

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

Upvotes: 0

Ketan Malhotra
Ketan Malhotra

Reputation: 1250

When you declare the method, just let it have a default value for the array parameter. 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;
}

Upvotes: 0

splash58
splash58

Reputation: 26153

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

Upvotes: 1

Related Questions