Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7007501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:36:55+00:00 2026-05-27T21:36:55+00:00

How does PHP array_keys do the search for value? Example: $array2 = array(xyz, xyz,

  • 0

How does PHP array_keys do the search for value?

Example:

$array2 = array("xyz", "xyz", "abc", "abc", "xyz", "xyz", "text", "abc", "text");

print_r(array_keys($array2,"abc"));

Since they are key,value pairs. I am guessing PHP to do a search based on hash rather than iterate through each of the key-pairs in the array.

Any ‘clarity thoughts’ on this?

Question inspired by this question: How to get the keys of empty elements in an array if the corresponding element in a similar-sized array is a number (without iteration)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:36:56+00:00Added an answer on May 27, 2026 at 9:36 pm

    In the php source, they iterate through each key and value, one by one.
    https://github.com/php/php-src/blob/master/ext/standard/array.c#L2439

    /* {{{ proto array array_keys(array input [, mixed search_value[, bool strict]])
       Return just the keys from the input array, optionally only for the specified search_value */
    PHP_FUNCTION(array_keys)
    {
        zval *input,                /* Input array */
             *search_value = NULL,  /* Value to search for */
             **entry,               /* An entry in the input array */
               res,                 /* Result of comparison */
              *new_val;             /* New value */
        int    add_key;             /* Flag to indicate whether a key should be added */
        char  *string_key;          /* String key */
        uint   string_key_len;
        ulong  num_key;             /* Numeric key */
        zend_bool strict = 0;       /* do strict comparison */
        HashPosition pos;
        int (*is_equal_func)(zval *, zval *, zval * TSRMLS_DC) = is_equal_function;
    
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|zb", &input, &search_value, &strict) == FAILURE) {
            return;
        }
    
        if (strict) {
            is_equal_func = is_identical_function;
        }
    
        /* Initialize return array */
        if (search_value != NULL) {
            array_init(return_value);
        } else {
            array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(input)));
        }
        add_key = 1;
    
        /* Go through input array and add keys to the return array */
        zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos);
        while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void **)&entry, &pos) == SUCCESS) {
            if (search_value != NULL) {
                is_equal_func(&res, search_value, *entry TSRMLS_CC);
                add_key = zval_is_true(&res);
            }
    
            if (add_key) {
                MAKE_STD_ZVAL(new_val);
    
                switch (zend_hash_get_current_key_ex(Z_ARRVAL_P(input), &string_key, &string_key_len, &num_key, 1, &pos)) {
                    case HASH_KEY_IS_STRING:
                        ZVAL_STRINGL(new_val, string_key, string_key_len - 1, 0);
                        zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &new_val, sizeof(zval *), NULL);
                        break;
    
                    case HASH_KEY_IS_LONG:
                        Z_TYPE_P(new_val) = IS_LONG;
                        Z_LVAL_P(new_val) = num_key;
                        zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &new_val, sizeof(zval *), NULL);
                        break;
                }
            }
    
            zend_hash_move_forward_ex(Z_ARRVAL_P(input), &pos);
        }
    }
    /* }}} */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does python have a function like call_user_func() in PHP? PHP Version: call_user_func(array($object,$methodName),$parameters) How do
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
I wrote this function to get a subset of an array. Does php have
Okay so the array is $nv = array(); $nv[$kk] = $value; And index key
Does count() really count the all the elements of a PHP array, or is
Can an array key in PHP be a string with embedded zero-bytes? I wanted
Does PHP have a method of having auto-generated class variables? I think I've seen
Does PHP expose Apache's Mime-Type to extension mapping in any way shape or form.
does PHP support something like ampersand in bash (forking)? Let's say I wanted to
How does PHP handle client connection while doing sleeps? Or put another way ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.