PHP类型转换和数组参考

时间:2019-03-28 11:08:07

标签: php arrays casting

我在PHP OOPS代码中遇到问题。

我的代码是:

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');

我的问题是:

当我打印print_r($this->client_general);时,它可以打印$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
        )

)

当我打印print_r($setlected_arr);

$this->client_general

我认为它也应该指向$client_general数组。

我该怎么做?

2 个答案:

答案 0 :(得分:3)

print_r($this->$setlected_arr);

答案 1 :(得分:2)

尝试一下,它为您服务。

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');

您要通过字符串创建动态变量的示例在这里:

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

我的编译输出: enter image description here