com php-src: array_column() - Use entire subject array when NULL passed for second param.: ext/standard/array.c ext/standard/tests/array/array_colum
n_variant.phpt
| From: | Sara Golemon | Date: | Mon, 22 Apr 2013 23:19:21 +0000 |
| Subject: | com php-src: array_column() - Use entire subject array when NULL passed for second param.: ext/standard/array.c ext/standard/tests/array/array_colum n_variant.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-71979@lists.php.net to get a copy of this message | ||
Commit: 734e165d4e427feca9a736b62832a2ff287a22c9
Author: Sara Golemon <pollita@php.net> Mon, 22 Apr 2013 16:19:21 -0700
Parents: 1a03bd5dee97a0f8b9e74b7f8db5231abd8cc7e4
Branches: PHP-5.5 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=734e165d4e427feca9a736b62832a2ff287a22c9
Log:
array_column() - Use entire subject array when NULL passed for second param.
This starts to look like array_values(), except that you can reindex the arrays
using the third parameter.
Changed paths:
M ext/standard/array.c
A ext/standard/tests/array/array_column_variant.phpt
Diff:
diff --git a/ext/standard/array.c b/ext/standard/array.c
index 6f769da..cfe9be8 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -2531,7 +2531,6 @@ static inline
zend_bool array_column_param_helper(zval **param,
const char *name TSRMLS_DC) {
switch (Z_TYPE_PP(param)) {
- case IS_NULL:
case IS_DOUBLE:
convert_to_long_ex(param);
/* fallthrough */
@@ -2555,15 +2554,15 @@ zend_bool array_column_param_helper(zval **param,
value_key and optionally indexed by the index_key */
PHP_FUNCTION(array_column)
{
- zval **zcolumn, **zkey = NULL, **data;
+ zval **zcolumn = NULL, **zkey = NULL, **data;
HashTable *arr_hash;
HashPosition pointer;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "hZ|Z!", &arr_hash,
&zcolumn, &zkey) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "hZ!|Z!", &arr_hash,
&zcolumn, &zkey) == FAILURE) {
return;
}
- if (!array_column_param_helper(zcolumn, "column" TSRMLS_CC) ||
+ if ((zcolumn && !array_column_param_helper(zcolumn, "column" TSRMLS_CC)) ||
(zkey && !array_column_param_helper(zkey, "index" TSRMLS_CC))) {
RETURN_FALSE;
}
@@ -2581,8 +2580,12 @@ PHP_FUNCTION(array_column)
}
ht = Z_ARRVAL_PP(data);
- /* Skip if the value doesn't exist in our subarray */
- if ((Z_TYPE_PP(zcolumn) == IS_STRING) &&
+ if (!zcolumn) {
+ /* NULL column ID means use entire subarray as data */
+ zcolval = data;
+
+ /* Otherwise, skip if the value doesn't exist in our subarray */
+ } else if ((Z_TYPE_PP(zcolumn) == IS_STRING) &&
(zend_hash_find(ht, Z_STRVAL_PP(zcolumn), Z_STRLEN_PP(zcolumn) + 1, (void**)&zcolval) ==
FAILURE)) {
continue;
} else if ((Z_TYPE_PP(zcolumn) == IS_LONG) &&
diff --git a/ext/standard/tests/array/array_column_variant.phpt
b/ext/standard/tests/array/array_column_variant.phpt
new file mode 100644
index 0000000..0af0869
--- /dev/null
+++ b/ext/standard/tests/array/array_column_variant.phpt
@@ -0,0 +1,85 @@
+--TEST--
+Test array_column() function: variant functionality
+--FILE--
+<?php
+/* Array from Bug Request #64493 test script */
+$rows = array(
+ 456 => array('id' => '3', 'title' => 'Foo',
'date' => '2013-03-25'),
+ 457 => array('id' => '5', 'title' => 'Bar',
'date' => '2012-05-20'),
+);
+
+echo "-- pass null as second parameter to get back all columns indexed by third parameter
--\n";
+var_dump(array_column($rows, null, 'id'));
+
+echo "-- pass null as second parameter and bogus third param to get back zero-indexed array of
all columns --\n";
+var_dump(array_column($rows, null, 'foo'));
+
+echo "-- pass null as second parameter and no third param to get back array_values(input)
--\n";
+var_dump(array_column($rows, null));
+
+echo "Done\n";
+--EXPECTF--
+-- pass null as second parameter to get back all columns indexed by third parameter --
+array(2) {
+ [3]=>
+ array(3) {
+ ["id"]=>
+ string(1) "3"
+ ["title"]=>
+ string(3) "Foo"
+ ["date"]=>
+ string(10) "2013-03-25"
+ }
+ [5]=>
+ array(3) {
+ ["id"]=>
+ string(1) "5"
+ ["title"]=>
+ string(3) "Bar"
+ ["date"]=>
+ string(10) "2012-05-20"
+ }
+}
+-- pass null as second parameter and bogus third param to get back zero-indexed array of all
columns --
+array(2) {
+ [0]=>
+ array(3) {
+ ["id"]=>
+ string(1) "3"
+ ["title"]=>
+ string(3) "Foo"
+ ["date"]=>
+ string(10) "2013-03-25"
+ }
+ [1]=>
+ array(3) {
+ ["id"]=>
+ string(1) "5"
+ ["title"]=>
+ string(3) "Bar"
+ ["date"]=>
+ string(10) "2012-05-20"
+ }
+}
+-- pass null as second parameter and no third param to get back array_values(input) --
+array(2) {
+ [0]=>
+ array(3) {
+ ["id"]=>
+ string(1) "3"
+ ["title"]=>
+ string(3) "Foo"
+ ["date"]=>
+ string(10) "2013-03-25"
+ }
+ [1]=>
+ array(3) {
+ ["id"]=>
+ string(1) "5"
+ ["title"]=>
+ string(3) "Bar"
+ ["date"]=>
+ string(10) "2012-05-20"
+ }
+}
+Done