Changeset 28900
- Timestamp:
- 06/29/2014 09:25:21 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r28894 r28900 2883 2883 * Pluck a certain field out of each object in a list. 2884 2884 * 2885 * This has the same functionality and prototype of 2886 * array_column() (PHP 5.5) but also supports objects. 2887 * 2885 2888 * @since 3.1.0 2886 2889 * 2887 2890 * @param array $list A list of objects or arrays 2888 2891 * @param int|string $field A field from the object to place instead of the entire object 2892 * @param int|string $index_key A field from the object to use as keys for the new array 2889 2893 * @return array 2890 2894 */ 2891 function wp_list_pluck( $list, $field ) { 2892 foreach ( $list as $key => $value ) { 2893 if ( is_object( $value ) ) 2894 $list[ $key ] = $value->$field; 2895 else 2896 $list[ $key ] = $value[ $field ]; 2897 } 2898 2899 return $list; 2895 function wp_list_pluck( $list, $field, $index_key = null ) { 2896 if ( ! $index_key ) { 2897 // This is simple. Could at some point wrap array_column() 2898 // if we knew we had an array of arrays. 2899 foreach ( $list as $key => $value ) { 2900 if ( is_object( $value ) ) { 2901 $list[ $key ] = $value->$field; 2902 } else { 2903 $list[ $key ] = $value[ $field ]; 2904 } 2905 } 2906 return $list; 2907 } 2908 2909 // When index_key is not set for a particular item, 2910 // push the value to the end of the stack. 2911 // This is how array_column() behaves. 2912 $newlist = array(); 2913 foreach ( $list as $value ) { 2914 if ( is_object( $value ) ) { 2915 if ( isset( $value->$index_key ) ) { 2916 $newlist[ $value->$index_key ] = $value->$field; 2917 } else { 2918 $newlist[] = $value->$field; 2919 } 2920 } else { 2921 if ( isset( $value[ $index_key ] ) ) { 2922 $newlist[ $value[ $index_key ] ] = $value[ $field ]; 2923 } else { 2924 $newlist[] = $value[ $field ]; 2925 } 2926 } 2927 } 2928 2929 return $newlist; 2900 2930 } 2901 2931 -
trunk/tests/phpunit/tests/functions/listFilter.php
r25002 r28900 12 12 function setUp() { 13 13 parent::setUp(); 14 $this->array_list['foo'] = array( 'name' => 'foo', ' field1' => true, 'field2' => true, 'field3' => true, 'field4' => array( 'red' ) );15 $this->array_list['bar'] = array( 'name' => 'bar', ' field1' => true, 'field2' => true, 'field3' => false, 'field4' => array( 'green' ) );16 $this->array_list['baz'] = array( 'name' => 'baz', ' field1' => true, 'field2' => false, 'field3' => false, 'field4' => array( 'blue' ) );14 $this->array_list['foo'] = array( 'name' => 'foo', 'id' => 'f', 'field1' => true, 'field2' => true, 'field3' => true, 'field4' => array( 'red' ) ); 15 $this->array_list['bar'] = array( 'name' => 'bar', 'id' => 'b', 'field1' => true, 'field2' => true, 'field3' => false, 'field4' => array( 'green' ) ); 16 $this->array_list['baz'] = array( 'name' => 'baz', 'id' => 'z', 'field1' => true, 'field2' => false, 'field3' => false, 'field4' => array( 'blue' ) ); 17 17 foreach ( $this->array_list as $key => $value ) { 18 18 $this->object_list[ $key ] = (object) $value; … … 67 67 } 68 68 69 /** 70 * @ticket 28666 71 */ 72 function test_wp_list_pluck_index_key() { 73 $list = wp_list_pluck( $this->array_list, 'name', 'id' ); 74 $this->assertEquals( array( 'f' => 'foo', 'b' => 'bar', 'z' => 'baz' ), $list ); 75 } 76 77 /** 78 * @ticket 28666 79 */ 80 function test_wp_list_pluck_object_index_key() { 81 $list = wp_list_pluck( $this->object_list, 'name', 'id' ); 82 $this->assertEquals( array( 'f' => 'foo', 'b' => 'bar', 'z' => 'baz' ), $list ); 83 } 84 85 /** 86 * @ticket 28666 87 */ 88 function test_wp_list_pluck_missing_index_key() { 89 $list = wp_list_pluck( $this->array_list, 'name', 'nonexistent' ); 90 $this->assertEquals( array( 0 => 'foo', 1 => 'bar', 2 => 'baz' ), $list ); 91 } 92 93 /** 94 * @ticket 28666 95 */ 96 function test_wp_list_pluck_partial_missing_index_key() { 97 $array_list = $this->array_list; 98 unset( $array_list[ 'bar']['id'] ); 99 $list = wp_list_pluck( $array_list, 'name', 'id' ); 100 $this->assertEquals( array( 'f' => 'foo', 0 => 'bar', 'z' => 'baz' ), $list ); 101 } 102 103 /** 104 * @ticket 28666 105 */ 106 function test_wp_list_pluck_mixed_index_key() { 107 $mixed_list = $this->array_list; 108 $mixed_list['bar'] = (object) $mixed_list['bar']; 109 $list = wp_list_pluck( $mixed_list, 'name', 'id' ); 110 $this->assertEquals( array( 'f' => 'foo', 'b' => 'bar', 'z' => 'baz' ), $list ); 111 } 112 69 113 function test_filter_object_list_nested_array_and() { 70 114 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' );
Note: See TracChangeset
for help on using the changeset viewer.