@@ -6915,8 +6915,8 @@ PHP_FUNCTION(array_key_exists)
69156915}
69166916/* }}} */
69176917
6918- /* {{{ Helper function to get a nested value from array using an array of segments */
6919- static zval * array_get_nested_from_hash (HashTable * ht , HashTable * segments )
6918+ /* {{{ Helper function to get a nested value from array using an array of path segments */
6919+ static zval * array_get_nested (HashTable * ht , HashTable * path )
69206920{
69216921 zval * segment_val ;
69226922 zval * current ;
@@ -6925,12 +6925,12 @@ static zval* array_get_nested_from_hash(HashTable *ht, HashTable *segments)
69256925 uint32_t num_segments ;
69266926
69276927 current_ht = ht ;
6928- num_segments = zend_hash_num_elements (segments );
6928+ num_segments = zend_hash_num_elements (path );
69296929
6930- /* Iterate through each segment in the array */
6930+ /* Iterate through each segment in the path array */
69316931 for (idx = 0 ; idx < num_segments ; idx ++ ) {
69326932 /* Get the segment at the current index */
6933- segment_val = zend_hash_index_find (segments , idx );
6933+ segment_val = zend_hash_index_find (path , idx );
69346934
69356935 if (segment_val == NULL ) {
69366936 /* Missing segment in array */
@@ -6961,160 +6961,54 @@ static zval* array_get_nested_from_hash(HashTable *ht, HashTable *segments)
69616961 current_ht = Z_ARRVAL_P (current );
69626962 }
69636963
6964- /* Empty segments array */
6964+ /* Empty path array */
69656965 return NULL ;
69666966}
69676967/* }}} */
69686968
6969- /* {{{ Helper function to get a nested value from array using dot notation string */
6970- static zval * array_get_nested_from_string (HashTable * ht , const char * key , size_t key_len )
6971- {
6972- const char * segment_start ;
6973- const char * dot ;
6974- size_t segment_len ;
6975- size_t remaining_len ;
6976- zval * current ;
6977- HashTable * current_ht ;
6978- zend_string * segment ;
6979-
6980- current_ht = ht ;
6981- segment_start = key ;
6982- remaining_len = key_len ;
6983-
6984- /* Iterate through each dot-separated segment */
6985- while (remaining_len > 0 ) {
6986- /* Find the next dot */
6987- dot = memchr (segment_start , '.' , remaining_len );
6988-
6989- if (dot == NULL ) {
6990- /* Last segment */
6991- segment_len = remaining_len ;
6992- } else {
6993- segment_len = dot - segment_start ;
6994- }
6995-
6996- /* Look up the current segment */
6997- segment = zend_string_init (segment_start , segment_len , 0 );
6998- current = zend_symtable_find (current_ht , segment );
6999- zend_string_release (segment );
7000-
7001- /* If this is the last segment, return the result */
7002- if (dot == NULL ) {
7003- return current ;
7004- }
7005-
7006- /* Check if the segment exists and is an array for next iteration */
7007- if (current == NULL || Z_TYPE_P (current ) != IS_ARRAY ) {
7008- return NULL ;
7009- }
7010-
7011- /* Move to the next segment */
7012- current_ht = Z_ARRVAL_P (current );
7013- segment_start = dot + 1 ;
7014- remaining_len = remaining_len - segment_len - 1 ;
7015- }
7016-
7017- return NULL ;
7018- }
7019- /* }}} */
7020-
7021- /* {{{ Retrieves a value from a deeply nested array using "dot" notation */
6969+ /* {{{ Retrieves a value from a deeply nested array using an array path */
70226970PHP_FUNCTION (array_get )
70236971{
70246972 zval * array ;
7025- zval * key = NULL ;
6973+ zval * path ;
70266974 zval * default_value = NULL ;
70276975 zval * result ;
7028- HashTable * ht ;
70296976
70306977 ZEND_PARSE_PARAMETERS_START (2 , 3 )
70316978 Z_PARAM_ARRAY (array )
7032- Z_PARAM_ZVAL_OR_NULL ( key )
6979+ Z_PARAM_ARRAY ( path )
70336980 Z_PARAM_OPTIONAL
70346981 Z_PARAM_ZVAL (default_value )
70356982 ZEND_PARSE_PARAMETERS_END ();
70366983
7037- /* If key is null, return the whole array */
7038- if (key == NULL || Z_TYPE_P (key ) == IS_NULL ) {
7039- RETURN_COPY (array );
7040- }
7041-
7042- ht = Z_ARRVAL_P (array );
7043-
7044- switch (Z_TYPE_P (key )) {
7045- case IS_ARRAY :
7046- /* Handle array keys (array of segments) */
7047- result = array_get_nested_from_hash (ht , Z_ARRVAL_P (key ));
7048-
7049- if (result != NULL ) {
7050- RETURN_COPY_DEREF (result );
7051- }
7052- break ;
7053-
7054- case IS_STRING :
7055- /* Handle string keys with dot notation */
7056- result = array_get_nested_from_string (ht , Z_STRVAL_P (key ), Z_STRLEN_P (key ));
7057-
7058- if (result != NULL ) {
7059- RETURN_COPY_DEREF (result );
7060- }
7061- break ;
7062-
7063- case IS_LONG :
7064- /* Handle integer keys (simple lookup) */
7065- result = zend_hash_index_find (ht , Z_LVAL_P (key ));
7066-
7067- if (result != NULL ) {
7068- RETURN_COPY_DEREF (result );
7069- }
7070- break ;
6984+ result = array_get_nested (Z_ARRVAL_P (array ), Z_ARRVAL_P (path ));
70716985
7072- default :
7073- zend_argument_type_error (2 , "must be of type string|int|array, %s given" , zend_zval_value_name (key ));
7074- RETURN_THROWS ();
6986+ if (result != NULL) {
6987+ RETURN_COPY_DEREF (result );
70756988 }
70766989
7077- /* Key not found, return default value */
6990+ /* Path not found, return default value */
70786991 if (default_value != NULL ) {
70796992 RETURN_COPY_DEREF (default_value );
70806993 }
70816994}
70826995/* }}} */
70836996
7084- /* {{{ Checks whether a given item exists in an array using "dot" notation */
6997+ /* {{{ Checks whether a given item exists in an array using an array path */
70856998PHP_FUNCTION (array_has )
70866999{
70877000 zval * array ;
7088- zval * key ;
7001+ zval * path ;
70897002 zval * result ;
7090- HashTable * ht ;
70917003
70927004 ZEND_PARSE_PARAMETERS_START (2 , 2 )
70937005 Z_PARAM_ARRAY (array )
7094- Z_PARAM_ZVAL ( key )
7006+ Z_PARAM_ARRAY ( path )
70957007 ZEND_PARSE_PARAMETERS_END ();
70967008
7097- ht = Z_ARRVAL_P (array );
7098-
7099- switch (Z_TYPE_P (key )) {
7100- case IS_ARRAY :
7101- /* Handle array keys (array of segments) */
7102- result = array_get_nested_from_hash (ht , Z_ARRVAL_P (key ));
7103- RETURN_BOOL (result != NULL );
7104-
7105- case IS_STRING :
7106- /* Handle string keys with dot notation */
7107- result = array_get_nested_from_string (ht , Z_STRVAL_P (key ), Z_STRLEN_P (key ));
7108- RETURN_BOOL (result != NULL );
7109-
7110- case IS_LONG :
7111- /* Handle integer keys (simple lookup) */
7112- RETURN_BOOL (zend_hash_index_exists (ht , Z_LVAL_P (key )));
7009+ result = array_get_nested (Z_ARRVAL_P (array ), Z_ARRVAL_P (path ));
71137010
7114- default :
7115- zend_argument_type_error (2 , "must be of type string|int|array, %s given" , zend_zval_value_name (key ));
7116- RETURN_THROWS ();
7117- }
7011+ RETURN_BOOL (result != NULL );
71187012}
71197013/* }}} */
71207014
0 commit comments