Access array using dynamic path

前端 未结 3 436
攒了一身酷
攒了一身酷 2020-12-11 05:58

I have an issue in accessing the array in php.

$path  = \"[\'a\'][\'b\'][\'c\']\";
$value = $array.$path;

In the above piece of code I hav

3条回答
  •  醉话见心
    2020-12-11 06:21

    I was hoping to find an elegant solution to nested array access without throwing undefined index errors, and this post hits high on google. I'm late to the party, but I wanted to weigh in for future visitors.

    A simple isset($array['a']['b']['c'] can safely check nested values, but you need to know the elements to access ahead of time. I like the dot notation for accessing multidimensional arrays, so I wrote a class of my own. It does require PHP 5.6.

    This class parses a string path written in dot-notation and safely accesses the nested values of the array or array-like object (implements ArrayAccess). It will return the value or NULL if not set.

    use ArrayAccess;
    
    class SafeArrayGetter implements \JsonSerializable {
    
    /**
     * @var array
     */
    private $data;
    
    /**
     * SafeArrayGetter constructor.
     *
     * @param array $data
     */
    public function __construct( array $data )
    {
        $this->data = $data;
    }
    
    /**
     * @param array $target
     * @param array ...$indices
     *
     * @return array|mixed|null
     */
    protected function safeGet( array $target, ...$indices )
    {
        $movingTarget = $target;
    
        foreach ( $indices as $index )
        {
            $isArray = is_array( $movingTarget ) || $movingTarget instanceof ArrayAccess;
            if ( ! $isArray || ! isset( $movingTarget[ $index ] ) ) return NULL;
    
            $movingTarget = $movingTarget[ $index ];
        }
    
        return $movingTarget;
    }
    
    /**
     * @param array ...$keys
     *
     * @return array|mixed|null
     */
    public function getKeys( ...$keys )
    {
        return static::safeGet( $this->data, ...$keys );
    }
    
    /**
     * 

    Access nested array index values by providing a dot notation access string.

    *

    Example: $safeArrayGetter->get('customer.paymentInfo.ccToken') == * $array['customer']['paymentInfo']['ccToken']

    * * @param $accessString * * @return array|mixed|null */ public function get( $accessString ) { $keys = $this->parseDotNotation( $accessString ); return $this->getKeys( ...$keys ); } /** * @param $string * * @return array */ protected function parseDotNotation( $string ) { return explode( '.', strval( $string ) ); } /** * @return array */ public function toArray() { return $this->data; } /** * @param int $options * @param int $depth * * @return string */ public function toJson( $options = 0, $depth = 512 ) { return json_encode( $this, $options, $depth ); } /** * @param array $data * * @return static */ public static function newFromArray( array $data ) { return new static( $data ); } /** * @param \stdClass $data * * @return static */ public static function newFromObject( \stdClass $data ) { return new static( json_decode( json_encode( $data ), TRUE ) ); } /** * Specify data which should be serialized to JSON * @link http://php.net/manual/en/jsonserializable.jsonserialize.php * @return array data which can be serialized by json_encode, * which is a value of any type other than a resource. * @since 5.4.0 */ function jsonSerialize() { return $this->toArray(); } }

提交回复
热议问题