Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7703379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:26:39+00:00 2026-05-31T23:26:39+00:00

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

  • 0

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(array_key_exists($key, $this->$array) && $this->$array[$key]):
        return $this->$array[$key];
      else:
        return $type;
      endif;
    }

The first check works (array_key_exists($key, $this->$array)). But the second check fails ($this->$array[$key]) 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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:26:40+00:00Added an answer on May 31, 2026 at 11:26 pm

    You are running into the dreaded treat string as an array trap, i.e.:

    $str = 'customer'; echo $str[0]; // prints c
    

    The $array[$key] portion of $this->$array[$key] is being interpreted as show me the nth index of string $array (if you pass customer it will try to access $this->c, which doesn’t exist). Assign the array to a variable first. Here is an example:

    class A
    {
        public $customer = array('dealerId'=>4, 'billFirstName'=>'Joe');
    
        public function keyCheck($key, $arrayName, $type = false)
        {
            $array = $this->$arrayName;
            if(array_key_exists($key, $array) && $array[$key]) {
                return $array[$key];
            } else {
                return $type;
            }
        }
    }
    
    $a = new A();
    echo $a->keyCheck('billFirstName', 'customer'); // Joe
    

    Alternatively, you could use the complex syntax: $this->{$arrayName}[$key] as suggested in Artjom’s answer.

    PHP 5.4 addresses this gotcha:

    [In PHP 5.4] Non-numeric string offsets – e.g. $a[‘foo’] where $a is a
    string – now return false on isset() and true on empty(), and produce
    a E_WARNING if you try to use them.

    This E_WARNING should help developers track down the cause much more quickly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.