$this->$array[$key] returning nothing when there is a value in place

I’m attempting to dynamically use a value in an array within an object.

In my particular case I have an array like this.

$this->customer = array(
     [dealerId] => 4
     [billFirstName] => Joe
     [billLastName] => Blo
     [billAddress1] => 1010s     
     [billAddress2] => 1020s
     [billCity] => Anytown
     [billState] => ST
     [billCountry] => USA
     [billPostalCode] => 11111
     [dEmail] => emailaddress
     [billPhone] => 8008008888
     [password] => password
     [distPrice] => 5
     [distCost] => 20);

$result = $this->keyCheck('dealerId', 'customer');

The method I’m using:

protected function keyCheck($key, $array, $type = false)
    {
      if([COLOR=#008000]array_key_exists($key, $this->$array)[/COLOR] &&[COLOR=#ff0000] $this->$array[$key][/COLOR]):
        return [COLOR=#ff0000]$this->$array[$key][/COLOR];
      else:
        return $type;
      endif;
    }

The first check works. But the second check fails even though there is a value held in that index of the array. I’ve proven that the array exists inside the keyCheck() method by using, print_r($this->$array); inside the method. And I know the value I’m looking for is available inside the method by using, print $this->$array[‘dealerId’];

Don’t get hung up on the names, or the methodology I’m using, what I’m really interested in is finding out how to address a value held in an array that is dynamically addressed in this way.

It’s probably so easy that I’ll be slapping my head once it’s revealed… :slight_smile:

NM, a solution was present. I’ll post it here for reference.

    protected function keyCheck($key, $array, $type = false)
    {
      $testArray = $this->$array; // We have to do this so that the method does not treat $array as a string in this construct: $this->$array[$key]

      if(array_key_exists($key, $testArray) && $testArray[$key]):
        return $this->$testArray[$key];
      else:
        return $type;
      endif;
    }

Hi PHP John,

You beat me too it… for your reference I solved it similarly;

 
<?php 
$o_Doit = null;
$o_Doit = new Doit();
$customer = null;
$customer = array(
    'dealerId' => 4
  ,'billFirstName' => 'Joe'
  ,'billLastName' => 'Blo'
  ,'billAddress1' => '1010s'
  ,'billAddress2' => '1020s'
  ,'billCity' => 'Anytown'
  ,'billState' => 'ST'
  ,'billCountry' => 'USA'
  ,'billPostalCode' => 11111
  ,'dEmail' => 'emailaddress'
  ,'billPhone' => 8008008888
  ,'password' => 'password'
  ,'distPrice' => 5
  ,'distCost' => 20
);
$result = null;
$result = $o_Doit->keyCheck('dealerId', $customer); 

if($result){
 echo $result;
} else {
  echo 'Result not found.';
}

class Doit{
  protected $p_array;
  public function __construct(){
    $this->p_array = null;
  }
   public function keyCheck($key, $array, $type = false){
    $this->p_array = $array;
    $this->{$key} = $this->p_array[$key]; //Dynamical set the property variable 
    if(array_key_exists($key, $this->p_array) && $this->p_array[$key]):
       return $this->p_array[$key]; 
    else:
      return $type;
    endif;
  }
}
?>

 

Regards,
Steve

Thanks!