<?php
class ArrayHelper
{
public static function getValue($array, $key, $default = null)
{
throw new InvalidParamException('Argument passed to getValue() must be an array or object.');
}
if ($key instanceof \Closure) {
return $key($array, $default);
}
foreach ($key as $keyPart) {
$array = static::getValue($array, $keyPart);
}
$key = $lastKey;
}
return $array[$key];
}
if (($pos = strrpos($key, '.')) !== false) { $array = static
::getValue($array, substr($key, 0, $pos), $default); $key = substr($key, $pos + 1); }
// this is expected to fail if the property does not exist, or __get() is not implemented
// it is not reliably possible to check whether a property is accessible beforehand
return $array->$key;
} else {
return $default;
}
}
}
$arrayHelper = new ArrayHelper();
print_r($arrayHelper::getValue($_POST, 'username')); echo "\n";
$post = ['username' => 'forecho'];
print_r($arrayHelper::getValue($post, 'username')); echo "\n";
class Address
{
public $street = '唐人街';
}
class User
{
public $username = 'forecho1';
public $address;
function __construct()
{
$this->address = new Address();
}
}
print_r($arrayHelper::getValue(new User
(), 'username')); echo "\n";
print_r($arrayHelper::getValue(new User
(), 'address.street')); echo "\n";
$versions = ['date' => '2016年12月19日', '1.0' => ['date' => '2016年12月18日']];
print_r($arrayHelper::getValue($versions, ['1.0', 'date']));
PD9waHAKCmNsYXNzIEFycmF5SGVscGVyCnsKCXB1YmxpYyBzdGF0aWMgZnVuY3Rpb24gZ2V0VmFsdWUoJGFycmF5LCAka2V5LCAkZGVmYXVsdCA9IG51bGwpCiAgICB7CiAgICAgICAgaWYgKCFpc19hcnJheSgkYXJyYXkpICYmICFpc19vYmplY3QoJGFycmF5KSkgewogICAgICAgICAgICB0aHJvdyBuZXcgSW52YWxpZFBhcmFtRXhjZXB0aW9uKCdBcmd1bWVudCBwYXNzZWQgdG8gZ2V0VmFsdWUoKSBtdXN0IGJlIGFuIGFycmF5IG9yIG9iamVjdC4nKTsKICAgICAgICB9CiAgICAgICAgaWYgKCRrZXkgaW5zdGFuY2VvZiBcQ2xvc3VyZSkgewogICAgICAgICAgICByZXR1cm4gJGtleSgkYXJyYXksICRkZWZhdWx0KTsKICAgICAgICB9CiAgICAgICAgaWYgKGlzX2FycmF5KCRrZXkpKSB7CiAgICAgICAgICAgICRsYXN0S2V5ID0gYXJyYXlfcG9wKCRrZXkpOwogICAgICAgICAgICBmb3JlYWNoICgka2V5IGFzICRrZXlQYXJ0KSB7CiAgICAgICAgICAgICAgICAkYXJyYXkgPSBzdGF0aWM6OmdldFZhbHVlKCRhcnJheSwgJGtleVBhcnQpOwogICAgICAgICAgICB9CiAgICAgICAgICAgICRrZXkgPSAkbGFzdEtleTsKICAgICAgICB9CiAgICAgICAgaWYgKGlzX2FycmF5KCRhcnJheSkgJiYgKGlzc2V0KCRhcnJheVska2V5XSkgfHwgYXJyYXlfa2V5X2V4aXN0cygka2V5LCAkYXJyYXkpKSApIHsKICAgICAgICAgICAgcmV0dXJuICRhcnJheVska2V5XTsKICAgICAgICB9CiAgICAgICAgaWYgKCgkcG9zID0gc3RycnBvcygka2V5LCAnLicpKSAhPT0gZmFsc2UpIHsKICAgICAgICAgICAgJGFycmF5ID0gc3RhdGljOjpnZXRWYWx1ZSgkYXJyYXksIHN1YnN0cigka2V5LCAwLCAkcG9zKSwgJGRlZmF1bHQpOwogICAgICAgICAgICAka2V5ID0gc3Vic3RyKCRrZXksICRwb3MgKyAxKTsKICAgICAgICB9CiAgICAgICAgaWYgKGlzX29iamVjdCgkYXJyYXkpKSB7CiAgICAgICAgICAgIC8vIHRoaXMgaXMgZXhwZWN0ZWQgdG8gZmFpbCBpZiB0aGUgcHJvcGVydHkgZG9lcyBub3QgZXhpc3QsIG9yIF9fZ2V0KCkgaXMgbm90IGltcGxlbWVudGVkCiAgICAgICAgICAgIC8vIGl0IGlzIG5vdCByZWxpYWJseSBwb3NzaWJsZSB0byBjaGVjayB3aGV0aGVyIGEgcHJvcGVydHkgaXMgYWNjZXNzaWJsZSBiZWZvcmVoYW5kCiAgICAgICAgICAgIHJldHVybiAkYXJyYXktPiRrZXk7CiAgICAgICAgfSBlbHNlaWYgKGlzX2FycmF5KCRhcnJheSkpIHsKICAgIC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