2

I am facing a problem in PHP OOPS code.

My code is:

class Settings
{

    private $client_addr = array(       
        'ClientID'              => array('maxlength'=>'10','IsNull'=>'n'),
        'ClientAddressType'     => array('maxlength'=>'12','IsNull'=>'y'),
        'ClientAddressLine1'    => array('maxlength'=>'30','IsNull'=>'y'),
        'ClientAddressLine2'    => array('maxlength'=>'30','IsNull'=>'y'),
        'ClientCounty'          => array('maxlength'=>'30','IsNull'=>'y'),
        'ClientCity'            => array('maxlength'=>'30','IsNull'=>'y'),
        'ClientState'           => array('maxlength'=>'2','IsNull'=>'y'),
        'ClientZip'             => array('maxlength'=>'9','IsNull'=>'y'),
    );
    private $client_general = array(
        'PayerID'                   => array('maxlength'=>'64','IsNull'=>'n'),  
        'ProviderID'                => array('maxlength'=>'50','IsNull'=>'n'),
        'ClientID'                  => array('maxlength'=>'10','IsNull'=>'n'),
        'ClientFirstName'           => array('maxlength'=>'30','IsNull'=>'n'),
        'ClientMiddleInitial'       => array('maxlength'=>'1','IsNull'=>'y'),
        'ClientLastName'            => array('maxlength'=>'30','IsNull'=>'n'),
    );

  function getSelectedArrayData($setlected_arr)
  {
      $setlected_arr = '$this->'."$setlected_arr";
      print_r($setlected_arr); //it prints a string '$this->client_general'
      print_r($this->client_general);//it prints $client_general array data
  }

}

$settings = new Settings();

$settings->getSelectedArrayData('client_general');

My problem is:

When I print print_r($this->client_general); it's printed $client_general array that is okay.

Array
(
    [PayerID] => Array
        (
            [maxlength] => 64
            [IsNull] => n
        )

    [ProviderID] => Array
        (
            [maxlength] => 50
            [IsNull] => n
        )

    [ClientID] => Array
        (
            [maxlength] => 10
            [IsNull] => n
        )

    [ClientFirstName] => Array
        (
            [maxlength] => 30
            [IsNull] => n
        )

    [ClientMiddleInitial] => Array
        (
            [maxlength] => 1
            [IsNull] => y
        )

    [ClientLastName] => Array
        (
            [maxlength] => 30
            [IsNull] => n
        )

)

When I print print_r($setlected_arr); It's printed

$this->client_general

I wnat that it should also point to the $client_general array.

How I can do it?

2 Answers 2

3
print_r($this->$setlected_arr);
Sign up to request clarification or add additional context in comments.

Comments

2

Try this, Its for you.

class Settings {

    private $client_addr = array(
        'ClientID' => array('maxlength' => '10', 'IsNull' => 'n'),
        'ClientAddressType' => array('maxlength' => '12', 'IsNull' => 'y'),
        'ClientAddressLine1' => array('maxlength' => '30', 'IsNull' => 'y'),
        'ClientAddressLine2' => array('maxlength' => '30', 'IsNull' => 'y'),
        'ClientCounty' => array('maxlength' => '30', 'IsNull' => 'y'),
        'ClientCity' => array('maxlength' => '30', 'IsNull' => 'y'),
        'ClientState' => array('maxlength' => '2', 'IsNull' => 'y'),
        'ClientZip' => array('maxlength' => '9', 'IsNull' => 'y'),
    );
    private $client_general = array(
        'PayerID' => array('maxlength' => '64', 'IsNull' => 'n'),
        'ProviderID' => array('maxlength' => '50', 'IsNull' => 'n'),
        'ClientID' => array('maxlength' => '10', 'IsNull' => 'n'),
        'ClientFirstName' => array('maxlength' => '30', 'IsNull' => 'n'),
        'ClientMiddleInitial' => array('maxlength' => '1', 'IsNull' => 'y'),
        'ClientLastName' => array('maxlength' => '30', 'IsNull' => 'n'),
    );

    function getSelectedArrayData($setlected_arr) {
        $setlected_arr = ${'this'}->${'setlected_arr'};
        print_r($setlected_arr); //it prints a string '$this->client_general'
        echo '<br/>';
        echo '<br/>';
        print_r($this->client_general); //it prints $client_general array data
    }

}

$settings = new Settings();

$settings->getSelectedArrayData('client_general');

Example for you is to create dynamic variable by string is here:

${'a' . 'b'} = 'hello there';
echo $ab; // hello there

My compiled output: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.