@@ -6913,8 +6913,8 @@ PHP_FUNCTION(array_key_exists)
69136913}
69146914/* }}} */
69156915
6916- /* {{{ Helper function to get a nested value from array using an array of segments */
6917- static zval * array_get_nested_from_hash (HashTable * ht , HashTable * segments )
6916+ /* {{{ Helper function to get a nested value from array using an array of path segments */
6917+ static zval * array_get_nested (HashTable * ht , HashTable * path )
69186918{
69196919 zval * segment_val ;
69206920 zval * current ;
@@ -6923,12 +6923,12 @@ static zval* array_get_nested_from_hash(HashTable *ht, HashTable *segments)
69236923 uint32_t num_segments ;
69246924
69256925 current_ht = ht ;
6926- num_segments = zend_hash_num_elements (segments );
6926+ num_segments = zend_hash_num_elements (path );
69276927
6928- /* Iterate through each segment in the array */
6928+ /* Iterate through each segment in the path array */
69296929 for (idx = 0 ; idx < num_segments ; idx ++ ) {
69306930 /* Get the segment at the current index */
6931- segment_val = zend_hash_index_find (segments , idx );
6931+ segment_val = zend_hash_index_find (path , idx );
69326932
69336933 if (segment_val == NULL ) {
69346934 /* Missing segment in array */
@@ -6959,160 +6959,54 @@ static zval* array_get_nested_from_hash(HashTable *ht, HashTable *segments)
69596959 current_ht = Z_ARRVAL_P (current );
69606960 }
69616961
6962- /* Empty segments array */
6962+ /* Empty path array */
69636963 return NULL ;
69646964}
69656965/* }}} */
69666966
6967- /* {{{ Helper function to get a nested value from array using dot notation string */
6968- static zval * array_get_nested_from_string (HashTable * ht , const char * key , size_t key_len )
6969- {
6970- const char * segment_start ;
6971- const char * dot ;
6972- size_t segment_len ;
6973- size_t remaining_len ;
6974- zval * current ;
6975- HashTable * current_ht ;
6976- zend_string * segment ;
6977-
6978- current_ht = ht ;
6979- segment_start = key ;
6980- remaining_len = key_len ;
6981-
6982- /* Iterate through each dot-separated segment */
6983- while (remaining_len > 0 ) {
6984- /* Find the next dot */
6985- dot = memchr (segment_start , '.' , remaining_len );
6986-
6987- if (dot == NULL ) {
6988- /* Last segment */
6989- segment_len = remaining_len ;
6990- } else {
6991- segment_len = dot - segment_start ;
6992- }
6993-
6994- /* Look up the current segment */
6995- segment = zend_string_init (segment_start , segment_len , 0 );
6996- current = zend_symtable_find (current_ht , segment );
6997- zend_string_release (segment );
6998-
6999- /* If this is the last segment, return the result */
7000- if (dot == NULL ) {
7001- return current ;
7002- }
7003-
7004- /* Check if the segment exists and is an array for next iteration */
7005- if (current == NULL || Z_TYPE_P (current ) != IS_ARRAY ) {
7006- return NULL ;
7007- }
7008-
7009- /* Move to the next segment */
7010- current_ht = Z_ARRVAL_P (current );
7011- segment_start = dot + 1 ;
7012- remaining_len = remaining_len - segment_len - 1 ;
7013- }
7014-
7015- return NULL ;
7016- }
7017- /* }}} */
7018-
7019- /* {{{ Retrieves a value from a deeply nested array using "dot" notation */
6967+ /* {{{ Retrieves a value from a deeply nested array using an array path */
70206968PHP_FUNCTION (array_get )
70216969{
70226970 zval * array ;
7023- zval * key = NULL ;
6971+ zval * path ;
70246972 zval * default_value = NULL ;
70256973 zval * result ;
7026- HashTable * ht ;
70276974
70286975 ZEND_PARSE_PARAMETERS_START (2 , 3 )
70296976 Z_PARAM_ARRAY (array )
7030- Z_PARAM_ZVAL_OR_NULL ( key )
6977+ Z_PARAM_ARRAY ( path )
70316978 Z_PARAM_OPTIONAL
70326979 Z_PARAM_ZVAL (default_value )
70336980 ZEND_PARSE_PARAMETERS_END ();
70346981
7035- /* If key is null, return the whole array */
7036- if (key == NULL || Z_TYPE_P (key ) == IS_NULL ) {
7037- RETURN_COPY (array );
7038- }
7039-
7040- ht = Z_ARRVAL_P (array );
7041-
7042- switch (Z_TYPE_P (key )) {
7043- case IS_ARRAY :
7044- /* Handle array keys (array of segments) */
7045- result = array_get_nested_from_hash (ht , Z_ARRVAL_P (key ));
7046-
7047- if (result != NULL ) {
7048- RETURN_COPY_DEREF (result );
7049- }
7050- break ;
7051-
7052- case IS_STRING :
7053- /* Handle string keys with dot notation */
7054- result = array_get_nested_from_string (ht , Z_STRVAL_P (key ), Z_STRLEN_P (key ));
7055-
7056- if (result != NULL ) {
7057- RETURN_COPY_DEREF (result );
7058- }
7059- break ;
7060-
7061- case IS_LONG :
7062- /* Handle integer keys (simple lookup) */
7063- result = zend_hash_index_find (ht , Z_LVAL_P (key ));
7064-
7065- if (result != NULL ) {
7066- RETURN_COPY_DEREF (result );
7067- }
7068- break ;
6982+ result = array_get_nested (Z_ARRVAL_P (array ), Z_ARRVAL_P (path ));
70696983
7070- default :
7071- zend_argument_type_error (2 , "must be of type string|int|array, %s given" , zend_zval_value_name (key ));
7072- RETURN_THROWS ();
6984+ if (result != NULL) {
6985+ RETURN_COPY_DEREF (result );
70736986 }
70746987
7075- /* Key not found, return default value */
6988+ /* Path not found, return default value */
70766989 if (default_value != NULL ) {
70776990 RETURN_COPY_DEREF (default_value );
70786991 }
70796992}
70806993/* }}} */
70816994
7082- /* {{{ Checks whether a given item exists in an array using "dot" notation */
6995+ /* {{{ Checks whether a given item exists in an array using an array path */
70836996PHP_FUNCTION (array_has )
70846997{
70856998 zval * array ;
7086- zval * key ;
6999+ zval * path ;
70877000 zval * result ;
7088- HashTable * ht ;
70897001
70907002 ZEND_PARSE_PARAMETERS_START (2 , 2 )
70917003 Z_PARAM_ARRAY (array )
7092- Z_PARAM_ZVAL ( key )
7004+ Z_PARAM_ARRAY ( path )
70937005 ZEND_PARSE_PARAMETERS_END ();
70947006
7095- ht = Z_ARRVAL_P (array );
7096-
7097- switch (Z_TYPE_P (key )) {
7098- case IS_ARRAY :
7099- /* Handle array keys (array of segments) */
7100- result = array_get_nested_from_hash (ht , Z_ARRVAL_P (key ));
7101- RETURN_BOOL (result != NULL );
7102-
7103- case IS_STRING :
7104- /* Handle string keys with dot notation */
7105- result = array_get_nested_from_string (ht , Z_STRVAL_P (key ), Z_STRLEN_P (key ));
7106- RETURN_BOOL (result != NULL );
7107-
7108- case IS_LONG :
7109- /* Handle integer keys (simple lookup) */
7110- RETURN_BOOL (zend_hash_index_exists (ht , Z_LVAL_P (key )));
7007+ result = array_get_nested (Z_ARRVAL_P (array ), Z_ARRVAL_P (path ));
71117008
7112- default :
7113- zend_argument_type_error (2 , "must be of type string|int|array, %s given" , zend_zval_value_name (key ));
7114- RETURN_THROWS ();
7115- }
7009+ RETURN_BOOL (result != NULL );
71167010}
71177011/* }}} */
71187012
0 commit comments