Make WordPress Core

Ticket #28666: 28666.diff

File 28666.diff, 1.2 KB (added by trepmal, 11 years ago)
  • wp-includes/functions.php

     
    28752875 *
    28762876 * @param array $list A list of objects or arrays
    28772877 * @param int|string $field A field from the object to place instead of the entire object
     2878 * @param int|string $index_key A field from the object to use as keys for the new array
    28782879 * @return array
    28792880 */
    2880 function wp_list_pluck( $list, $field ) {
     2881function wp_list_pluck( $list, $field, $index_key = null ) {
     2882        if ( $index_key ) {
     2883                $newlist = array();
     2884        } else {
     2885                $newlist = $list;
     2886        }
     2887
    28812888        foreach ( $list as $key => $value ) {
    2882                 if ( is_object( $value ) )
    2883                         $list[ $key ] = $value->$field;
    2884                 else
    2885                         $list[ $key ] = $value[ $field ];
     2889                if ( $index_key ) {
     2890                        if ( is_object( $value ) ) {
     2891                                $newkey = isset( $value->$index_key ) ? $value->$index_key : $key;
     2892                        } else {
     2893                                $newkey = isset( $value[ $index_key ] ) ? $value->$index_key : $key;
     2894                        }
     2895                } else {
     2896                        $newkey = $key;
     2897                }
     2898                if ( is_object( $value ) ) {
     2899                        $newlist[ $newkey ] = $value->$field;
     2900                } else {
     2901                        $newlist[ $newkey ] = $value[ $field ];
     2902                }
    28862903        }
    28872904
    2888         return $list;
     2905        return $newlist;
    28892906}
    28902907
    28912908/**