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 */
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;
}
}
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) &&
--- /dev/null
+--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