@ 2024-02-22T20:32:27Z <?php
function __( $str ) {
return $str;
}
function _doing_it_wrong( $func, $error_message, $version ) {
trigger_error( $error_message, E_USER_NOTICE );
}
class WP_List_Util {
/**
* The input array.
*
* @since 4.7.0
* @var array
*/
private $input = array();
/**
* The output array.
*
* @since 4.7.0
* @var array
*/
private $output = array();
/**
* Temporary arguments for sorting.
*
* @since 4.7.0
* @var string[]
*/
private $orderby = array();
/**
* Constructor.
*
* Sets the input array.
*
* @since 4.7.0
*
* @param array $input Array to perform operations on.
*/
public function __construct( $input ) {
$this->output = $input;
$this->input = $input;
}
public function pluck( $field, $index_key = null ) {
$newlist = array();
if ( ! $index_key ) {
/*
* This is simple. Could at some point wrap array_column()
* if we knew we had an array of arrays.
*/
foreach ( $this->output as $key => $value ) {
if ( is_object( $value ) ) {
$newlist[ $key ] = $value->$field;
} elseif ( is_array( $value ) ) {
$newlist[ $key ] = $value[ $field ];
} else {
_doing_it_wrong(
__METHOD__,
__( 'Values for the input array must be either objects or arrays.' ),
'6.2.0'
);
}
}
$this->output = $newlist;
return $this->output;
}
/*
* When index_key is not set for a particular item, push the value
* to the end of the stack. This is how array_column() behaves.
*/
foreach ( $this->output as $value ) {
if ( is_object( $value ) ) {
if ( isset( $value->$index_key ) ) {
$newlist[ $value->$index_key ] = $value->$field;
} else {
$newlist[] = $value->$field;
}
} elseif ( is_array( $value ) ) {
if ( isset( $value[ $index_key ] ) ) {
$newlist[ $value[ $index_key ] ] = $value[ $field ];
} else {
$newlist[] = $value[ $field ];
}
} else {
// _doing_it_wrong(
// __METHOD__,
// __( 'Values for the input array must be either objects or arrays.' ),
// '6.2.0'
// );
}
}
$this->output = $newlist;
return $this->output;
}
}
function wp_list_pluck( $input_list, $field, $index_key = null ) {
if ( ! is_array( $input_list ) ) {
return array();
}
$util = new WP_List_Util( $input_list );
return $util->pluck( $field, $index_key );
}
/*
* Test with an array of arrays.
*/
$input_list = array(
array( '123' => '456' ),
array( 'foo' => 'bar' ),
array( 'foo' => 'baz' ),
);
var_dump( array_column( $input_list, 'foo', null ) );
var_dump( wp_list_pluck( $input_list, 'foo', null ) );
/*
* Test with an array of objects.
*/
$input_list = array(
(object) array( '123' => '456' ),
(object) array( 'foo' => 'bar' ),
(object) array( 'foo' => 'baz' ),
);
var_dump( wp_list_pluck( $input_list, 'foo', null ) );
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.31 , 8.2.0 - 8.2.27 , 8.3.0 - 8.3.15 , 8.4.1 - 8.4.2 array(2) {
[0]=>
string(3) "bar"
[1]=>
string(3) "baz"
}
Warning: Undefined array key "foo" in /in/RtbZJ on line 62
array(3) {
[0]=>
NULL
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
}
Warning: Undefined property: stdClass::$foo in /in/RtbZJ on line 60
array(3) {
[0]=>
NULL
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
}
Output for 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.34 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 array(2) {
[0]=>
string(3) "bar"
[1]=>
string(3) "baz"
}
Notice: Undefined index: foo in /in/RtbZJ on line 62
array(3) {
[0]=>
NULL
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
}
Notice: Undefined property: stdClass::$foo in /in/RtbZJ on line 60
array(3) {
[0]=>
NULL
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
}
Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 Fatal error: Call to undefined function array_column() in /in/RtbZJ on line 127
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/RtbZJ on line 18
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/RtbZJ on line 18
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/RtbZJ on line 18
Process exited with code 255 . preferences:dark mode live preview
61.15 ms | 413 KiB | 5 Q