PHP multidimensional array search by value

前端 未结 23 3248
情歌与酒
情歌与酒 2020-11-21 04:45

I have an array where I want to search the uid and get the key of the array.

Examples

Assume we have the following 2-dimensional array:

<
23条回答
  •  别那么骄傲
    2020-11-21 05:45

    I know this was already answered, but I used this and extended it a little more in my code so that you didn't have search by only the uid. I just want to share it for anyone else who may need that functionality.

    Here's my example and please bare in mind this is my first answer. I took out the param array because I only needed to search one specific array, but you could easily add it in. I wanted to essentially search by more than just the uid.

    Also, in my situation there may be multiple keys to return as a result of searching by other fields that may not be unique.

     /**
         * @param array multidimensional 
         * @param string value to search for, ie a specific field name like name_first
         * @param string associative key to find it in, ie field_name
         * 
         * @return array keys.
         */
         function search_revisions($dataArray, $search_value, $key_to_search) {
            // This function will search the revisions for a certain value
            // related to the associative key you are looking for.
            $keys = array();
            foreach ($dataArray as $key => $cur_value) {
                if ($cur_value[$key_to_search] == $search_value) {
                    $keys[] = $key;
                }
            }
            return $keys;
        }
    

    Later, I ended up writing this to allow me to search for another value and associative key. So my first example allows you to search for a value in any specific associative key, and return all the matches.

    This second example shows you where a value ('Taylor') is found in a certain associative key (first_name) AND another value (true) is found in another associative key (employed), and returns all matches (Keys where people with first name 'Taylor' AND are employed).

    /**
     * @param array multidimensional 
     * @param string $search_value The value to search for, ie a specific 'Taylor'
     * @param string $key_to_search The associative key to find it in, ie first_name
     * @param string $other_matching_key The associative key to find in the matches for employed
     * @param string $other_matching_value The value to find in that matching associative key, ie true
     * 
     * @return array keys, ie all the people with the first name 'Taylor' that are employed.
     */
     function search_revisions($dataArray, $search_value, $key_to_search, $other_matching_value = null, $other_matching_key = null) {
        // This function will search the revisions for a certain value
        // related to the associative key you are looking for.
        $keys = array();
        foreach ($dataArray as $key => $cur_value) {
            if ($cur_value[$key_to_search] == $search_value) {
                if (isset($other_matching_key) && isset($other_matching_value)) {
                    if ($cur_value[$other_matching_key] == $other_matching_value) {
                        $keys[] = $key;
                    }
                } else {
                    // I must keep in mind that some searches may have multiple
                    // matches and others would not, so leave it open with no continues.
                    $keys[] = $key;
                }
            }
        }
        return $keys;
    }
    

    Use of function

    $data = array(
        array(
            'cust_group' => 6,
            'price' => 13.21,
            'price_qty' => 5
        ),
        array(
            'cust_group' => 8,
            'price' => 15.25,
            'price_qty' => 4
        ),
        array(
            'cust_group' => 8,
            'price' => 12.75,
            'price_qty' => 10
        )
    );
    
    $findKey = search_revisions($data,'8', 'cust_group', '10', 'price_qty');
    print_r($findKey);
    

    Result

    Array ( [0] => 2 ) 
    

提交回复
热议问题