var_dump - 如何从dump PHP中获取特定值

时间:2021-12-16 00:06:51

I know there is alot of information available in regards to PHP - var_dump my question is if I need to pick the particular information from the dump I am receiving from the API which is returning different array object....for example the below dump I need the information of realm under characterData Array how can I pick the that information and stored in String variable. Brief example of php code to explain my question will be highly appreciated...

我知道有很多关于PHP的信息 - var_dump我的问题是,如果我需要从我从API返回的转储中选择特定信息,它返回不同的数组对象....例如下面的转储我需要characterData下的域的信息数组如何选择该信息并存储在String变量中。用于解释我的问题的PHP代码的简要示例将受到高度赞赏...

  object(Character)[3]
   private 'name' => string 'XXXX' (length=6)
   private 'region' => string 'eu' (length=2)
   private 'realm' => string 'Defias Brotherhood' (length=18)
   private 'characterData' => 
   array (size=24)
  'lastModified' => float 1363345999000
  'name' => string 'Growar' (length=6)
  'realm' => string 'Defias Brotherhood' (length=18)
  'battlegroup' => string 'Rampage / Saccage' (length=17)

basically this information is coming from battlegroup api

基本上这些信息来自battlegroup api

    $armory = new BattlenetArmory('EU','Defias Brotherhood');
$armory->setLocale('ru_RU');

    // To reset back to default server locale
    $armory->setLocale(FALSE);
    //initialize the character to get the character object
    $character = $armory->getCharacter('XXXX');

2 个解决方案

#1


2  

If you don't have a getter for the field characterData, you won't have access to it because it is a private property, unless you use Reflection:

如果你没有字段characterData的getter,你将无法访问它,因为它是一个私有属性,除非你使用Reflection:

$ref = new ReflectionClass( 'Character');
$prop = $ref->getProperty( 'characterData');
$prop->setAccessible( true);
$array = $prop->getValue( $yourCharacterObject);
echo $array['realm'];

This can be tested with a simple class that mimics the OPs:

这可以通过模拟OP的简单类来测试:

class Character {
    private $characterData;

    public function __construct() {
        $this->characterData['realm'] = 'Defias Brotherhood';
    }
}

Currently, others have the proposed this solution:

目前,其他人已经提出了这个解决方案:

$yourCharacterObject = new Character();
// echo $yourCharacterObject->characterData['realm']; 

Which results in:

结果如下:

Fatal error: Cannot access private property Character::$characterData in X on line 11

致命错误:无法在第11行的X中访问私有属性Character :: $ characterData

However, Reflection will be able to grab the private value and display it without error, as shown in this demo, which uses the above class and code to print the desired property value.

但是,Reflection将能够获取私有值并显示它而不会出现错误,如此演示中所示,该演示使用上述类和代码来打印所需的属性值。

#2


0  

Character is a class that you create instance from - for example:

Character是您创建实例的类 - 例如:

$c = new Character();

since characterData is private you can't access it via $c->characterData

由于characterData是私有的,因此无法通过$ c-> characterData访问它

You need to use some public access function (like getCharacterData) or if you have access inside the object just changed it according to your needs

您需要使用一些公共访问功能(如getCharacterData),或者如果您有权访问对象内部,只需根据您的需要进行更改

#1


2  

If you don't have a getter for the field characterData, you won't have access to it because it is a private property, unless you use Reflection:

如果你没有字段characterData的getter,你将无法访问它,因为它是一个私有属性,除非你使用Reflection:

$ref = new ReflectionClass( 'Character');
$prop = $ref->getProperty( 'characterData');
$prop->setAccessible( true);
$array = $prop->getValue( $yourCharacterObject);
echo $array['realm'];

This can be tested with a simple class that mimics the OPs:

这可以通过模拟OP的简单类来测试:

class Character {
    private $characterData;

    public function __construct() {
        $this->characterData['realm'] = 'Defias Brotherhood';
    }
}

Currently, others have the proposed this solution:

目前,其他人已经提出了这个解决方案:

$yourCharacterObject = new Character();
// echo $yourCharacterObject->characterData['realm']; 

Which results in:

结果如下:

Fatal error: Cannot access private property Character::$characterData in X on line 11

致命错误:无法在第11行的X中访问私有属性Character :: $ characterData

However, Reflection will be able to grab the private value and display it without error, as shown in this demo, which uses the above class and code to print the desired property value.

但是,Reflection将能够获取私有值并显示它而不会出现错误,如此演示中所示,该演示使用上述类和代码来打印所需的属性值。

#2


0  

Character is a class that you create instance from - for example:

Character是您创建实例的类 - 例如:

$c = new Character();

since characterData is private you can't access it via $c->characterData

由于characterData是私有的,因此无法通过$ c-> characterData访问它

You need to use some public access function (like getCharacterData) or if you have access inside the object just changed it according to your needs

您需要使用一些公共访问功能(如getCharacterData),或者如果您有权访问对象内部,只需根据您的需要进行更改