SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question $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.

    PHP Code:
    $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:

    Code:
    protected function keyCheck($key, $array, $type = false)
        {
          if(array_key_exists($key, $this->$array) && $this->$array[$key]):
            return $this->$array[$key];
          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...
    John

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NM, a solution was present. I'll post it here for reference.

    PHP Code:
        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;
        } 
    John

  3. #3
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Hi PHP John,

    You beat me too it... for your reference I solved it similarly;
    PHP Code:
    <?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
    ictus==""

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!
    John


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •