Commit: a1876b6846898724f564208f78281dfd8ce07112
Author: Ben Ramsey <ram...@php.net> Fri, 11 Jan 2013 17:09:34 -0600
Parents: 5bc2854b37778d7f402db8bb9be9fe33df64258e
Branches: PHP-5.5 master
Link:
http://git.php.net/?p=php-src.git;a=commitdiff;h=a1876b6846898724f564208f78281dfd8ce07112
Log:
array_column: Set array_pluck as an alias for array_column
Changed paths:
M ext/standard/basic_functions.c
A ext/standard/tests/array/array_column_pluck_alias.phpt
A ext/standard/tests/array/array_column_pluck_alias_error.phpt
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 5e12352..6e1835d 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -3306,6 +3306,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(array_values,
arginfo_array_values)
PHP_FE(array_count_values,
arginfo_array_count_values)
PHP_FE(array_column,
arginfo_array_column)
+ PHP_FALIAS(array_pluck, array_column,
arginfo_array_column)
PHP_FE(array_reverse,
arginfo_array_reverse)
PHP_FE(array_reduce,
arginfo_array_reduce)
PHP_FE(array_pad,
arginfo_array_pad)
diff --git a/ext/standard/tests/array/array_column_pluck_alias.phpt
b/ext/standard/tests/array/array_column_pluck_alias.phpt
new file mode 100644
index 0000000..4bd251a
--- /dev/null
+++ b/ext/standard/tests/array/array_column_pluck_alias.phpt
@@ -0,0 +1,307 @@
+--TEST--
+Test array_pluck() function: basic functionality
+--FILE--
+<?php
+/* Prototype:
+ * array array_pluck(array $input, mixed $column_key[, mixed $index_key]);
+ * Description:
+ * Returns an array containing all the values from
+ * the specified "column" in a two-dimensional array.
+ */
+
+echo "*** Testing array_pluck() : basic functionality ***\n";
+/* Array representing a possible record set returned from a database */
+$records = array(
+ array(
+ 'id' => 1,
+ 'first_name' => 'John',
+ 'last_name' => 'Doe'
+ ),
+ array(
+ 'id' => 2,
+ 'first_name' => 'Sally',
+ 'last_name' => 'Smith'
+ ),
+ array(
+ 'id' => 3,
+ 'first_name' => 'Jane',
+ 'last_name' => 'Jones'
+ )
+);
+
+echo "-- first_name column from recordset --\n";
+var_dump(array_pluck($records, 'first_name'));
+
+echo "-- id column from recordset --\n";
+var_dump(array_pluck($records, 'id'));
+
+echo "-- last_name column from recordset, keyed by value from id column --\n";
+var_dump(array_pluck($records, 'last_name', 'id'));
+
+echo "-- last_name column from recordset, keyed by value from first_name
column --\n";
+var_dump(array_pluck($records, 'last_name', 'first_name'));
+
+echo "\n*** Testing multiple data types ***\n";
+$file = basename(__FILE__);
+$fh = fopen($file, 'r', true);
+$values = array(
+ array(
+ 'id' => 1,
+ 'value' => new stdClass
+ ),
+ array(
+ 'id' => 2,
+ 'value' => 34.2345
+ ),
+ array(
+ 'id' => 3,
+ 'value' => true
+ ),
+ array(
+ 'id' => 4,
+ 'value' => false
+ ),
+ array(
+ 'id' => 5,
+ 'value' => null
+ ),
+ array(
+ 'id' => 6,
+ 'value' => 1234
+ ),
+ array(
+ 'id' => 7,
+ 'value' => 'Foo'
+ ),
+ array(
+ 'id' => 8,
+ 'value' => $fh
+ )
+);
+var_dump(array_pluck($values, 'value'));
+var_dump(array_pluck($values, 'value', 'id'));
+
+echo "\n*** Testing numeric column keys ***\n";
+$numericCols = array(
+ array('aaa', '111'),
+ array('bbb', '222'),
+ array('ccc', '333')
+);
+var_dump(array_pluck($numericCols, 1));
+var_dump(array_pluck($numericCols, 1, 0));
+
+echo "\n*** Testing failure to find specified column ***\n";
+var_dump(array_pluck($numericCols, 2));
+var_dump(array_pluck($numericCols, 'foo'));
+var_dump(array_pluck($numericCols, 0, 'foo'));
+
+echo "\n*** Testing single dimensional array ***\n";
+$singleDimension = array('foo', 'bar', 'baz');
+var_dump(array_pluck($singleDimension, 1));
+
+echo "\n*** Testing columns not present in all rows ***\n";
+$mismatchedColumns = array(
+ array('a' => 'foo', 'b' => 'bar', 'e' => 'bbb'),
+ array('a' => 'baz', 'c' => 'qux', 'd' => 'aaa'),
+ array('a' => 'eee', 'b' => 'fff', 'e' => 'ggg'),
+);
+var_dump(array_pluck($mismatchedColumns, 'c'));
+var_dump(array_pluck($mismatchedColumns, 'c', 'a'));
+var_dump(array_pluck($mismatchedColumns, 'a', 'd'));
+var_dump(array_pluck($mismatchedColumns, 'a', 'e'));
+var_dump(array_pluck($mismatchedColumns, 'b'));
+var_dump(array_pluck($mismatchedColumns, 'b', 'a'));
+
+echo "\n*** Testing use of object converted to string ***\n";
+class Foo
+{
+ public function __toString()
+ {
+ return 'last_name';
+ }
+}
+class Bar
+{
+ public function __toString()
+ {
+ return 'first_name';
+ }
+}
+$f = new Foo();
+$b = new Bar();
+var_dump(array_pluck($records, $f));
+var_dump(array_pluck($records, $f, $b));
+
+echo "Done\n";
+?>
+--EXPECTF--
+*** Testing array_pluck() : basic functionality ***
+-- first_name column from recordset --
+array(3) {
+ [0]=>
+ string(4) "John"
+ [1]=>
+ string(5) "Sally"
+ [2]=>
+ string(4) "Jane"
+}
+-- id column from recordset --
+array(3) {
+ [0]=>
+ int(1)
+ [1]=>
+ int(2)
+ [2]=>
+ int(3)
+}
+-- last_name column from recordset, keyed by value from id column --
+array(3) {
+ [1]=>
+ string(3) "Doe"
+ [2]=>
+ string(5) "Smith"
+ [3]=>
+ string(5) "Jones"
+}
+-- last_name column from recordset, keyed by value from first_name column --
+array(3) {
+ ["John"]=>
+ string(3) "Doe"
+ ["Sally"]=>
+ string(5) "Smith"
+ ["Jane"]=>
+ string(5) "Jones"
+}
+
+*** Testing multiple data types ***
+array(8) {
+ [0]=>
+ object(stdClass)#1 (0) {
+ }
+ [1]=>
+ float(34.2345)
+ [2]=>
+ bool(true)
+ [3]=>
+ bool(false)
+ [4]=>
+ NULL
+ [5]=>
+ int(1234)
+ [6]=>
+ string(3) "Foo"
+ [7]=>
+ resource(5) of type (stream)
+}
+array(8) {
+ [1]=>
+ object(stdClass)#1 (0) {
+ }
+ [2]=>
+ float(34.2345)
+ [3]=>
+ bool(true)
+ [4]=>
+ bool(false)
+ [5]=>
+ NULL
+ [6]=>
+ int(1234)
+ [7]=>
+ string(3) "Foo"
+ [8]=>
+ resource(5) of type (stream)
+}
+
+*** Testing numeric column keys ***
+array(3) {
+ [0]=>
+ string(3) "111"
+ [1]=>
+ string(3) "222"
+ [2]=>
+ string(3) "333"
+}
+array(3) {
+ ["aaa"]=>
+ string(3) "111"
+ ["bbb"]=>
+ string(3) "222"
+ ["ccc"]=>
+ string(3) "333"
+}
+
+*** Testing failure to find specified column ***
+array(0) {
+}
+array(0) {
+}
+array(3) {
+ [0]=>
+ string(3) "aaa"
+ [1]=>
+ string(3) "bbb"
+ [2]=>
+ string(3) "ccc"
+}
+
+*** Testing single dimensional array ***
+array(0) {
+}
+
+*** Testing columns not present in all rows ***
+array(1) {
+ [0]=>
+ string(3) "qux"
+}
+array(1) {
+ ["baz"]=>
+ string(3) "qux"
+}
+array(3) {
+ [0]=>
+ string(3) "foo"
+ ["aaa"]=>
+ string(3) "baz"
+ [1]=>
+ string(3) "eee"
+}
+array(3) {
+ ["bbb"]=>
+ string(3) "foo"
+ [0]=>
+ string(3) "baz"
+ ["ggg"]=>
+ string(3) "eee"
+}
+array(2) {
+ [0]=>
+ string(3) "bar"
+ [1]=>
+ string(3) "fff"
+}
+array(2) {
+ ["foo"]=>
+ string(3) "bar"
+ ["eee"]=>
+ string(3) "fff"
+}
+
+*** Testing use of object converted to string ***
+array(3) {
+ [0]=>
+ string(3) "Doe"
+ [1]=>
+ string(5) "Smith"
+ [2]=>
+ string(5) "Jones"
+}
+array(3) {
+ ["John"]=>
+ string(3) "Doe"
+ ["Sally"]=>
+ string(5) "Smith"
+ ["Jane"]=>
+ string(5) "Jones"
+}
+Done
diff --git a/ext/standard/tests/array/array_column_pluck_alias_error.phpt
b/ext/standard/tests/array/array_column_pluck_alias_error.phpt
new file mode 100644
index 0000000..2f98ff5
--- /dev/null
+++ b/ext/standard/tests/array/array_column_pluck_alias_error.phpt
@@ -0,0 +1,98 @@
+--TEST--
+Test array_pluck() function: error conditions
+--FILE--
+<?php
+/* Prototype:
+ * array array_pluck(array $input, mixed $column_key[, mixed $index_key]);
+ * Description:
+ * Returns an array containing all the values from
+ * the specified "column" in a two-dimensional array.
+ */
+
+echo "*** Testing array_pluck() : error conditions ***\n";
+
+echo "\n-- Testing array_pluck() function with Zero arguments --\n";
+var_dump(array_pluck());
+
+echo "\n-- Testing array_pluck() function with One argument --\n";
+var_dump(array_pluck(array()));
+
+echo "\n-- Testing array_pluck() function with string as first parameter --\n";
+var_dump(array_pluck('foo', 0));
+
+echo "\n-- Testing array_pluck() function with int as first parameter --\n";
+var_dump(array_pluck(1, 'foo'));
+
+echo "\n-- Testing array_pluck() column key parameter should be a string or an
integer (testing bool) --\n";
+var_dump(array_pluck(array(), true));
+
+echo "\n-- Testing array_pluck() column key parameter should be a string or
integer (testing float) --\n";
+var_dump(array_pluck(array(), 2.3));
+
+echo "\n-- Testing array_pluck() column key parameter should be a string or
integer (testing array) --\n";
+var_dump(array_pluck(array(), array()));
+
+echo "\n-- Testing array_pluck() index key parameter should be a string or an
integer (testing bool) --\n";
+var_dump(array_pluck(array(), 'foo', true));
+
+echo "\n-- Testing array_pluck() index key parameter should be a string or
integer (testing float) --\n";
+var_dump(array_pluck(array(), 'foo', 2.3));
+
+echo "\n-- Testing array_pluck() index key parameter should be a string or
integer (testing array) --\n";
+var_dump(array_pluck(array(), 'foo', array()));
+
+echo "Done\n";
+?>
+--EXPECTF--
+*** Testing array_pluck() : error conditions ***
+
+-- Testing array_pluck() function with Zero arguments --
+
+Warning: array_pluck() expects at least 2 parameters, 0 given in %s on line %d
+NULL
+
+-- Testing array_pluck() function with One argument --
+
+Warning: array_pluck() expects at least 2 parameters, 1 given in %s on line %d
+NULL
+
+-- Testing array_pluck() function with string as first parameter --
+
+Warning: array_pluck() expects parameter 1 to be array, string given in %s on
line %d
+NULL
+
+-- Testing array_pluck() function with int as first parameter --
+
+Warning: array_pluck() expects parameter 1 to be array, integer given in %s on
line %d
+NULL
+
+-- Testing array_pluck() column key parameter should be a string or an integer
(testing bool) --
+
+Warning: array_pluck(): The column key should be either a string or an integer
in %s on line %d
+bool(false)
+
+-- Testing array_pluck() column key parameter should be a string or integer
(testing float) --
+
+Warning: array_pluck(): The column key should be either a string or an integer
in %s on line %d
+bool(false)
+
+-- Testing array_pluck() column key parameter should be a string or integer
(testing array) --
+
+Warning: array_pluck(): The column key should be either a string or an integer
in %s on line %d
+bool(false)
+
+-- Testing array_pluck() index key parameter should be a string or an integer
(testing bool) --
+
+Warning: array_pluck(): The index key should be either a string or an integer
in %s on line %d
+bool(false)
+
+-- Testing array_pluck() index key parameter should be a string or integer
(testing float) --
+
+Warning: array_pluck(): The index key should be either a string or an integer
in %s on line %d
+bool(false)
+
+-- Testing array_pluck() index key parameter should be a string or integer
(testing array) --
+
+Warning: array_pluck(): The index key should be either a string or an integer
in %s on line %d
+bool(false)
+Done
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php