From d5ea8ff20fbdcbaffefe2b372c7bf319ca271622 Mon Sep 17 00:00:00 2001 From: Sara Golemon Date: Wed, 24 Apr 2013 09:06:06 -0700 Subject: [PATCH] Add PHP 5.5's array_column() function http://wiki.php.net/rfc/array_column --- hphp/idl/array.idl.json | 28 ++- hphp/runtime/ext/ext_array.cpp | 59 +++++- hphp/runtime/ext/ext_array.ext_hhvm.cpp | 36 ++++ hphp/runtime/ext/ext_array.ext_hhvm.h | 13 ++ hphp/runtime/ext/ext_array.h | 2 + hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.cpp | 29 +++ hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.h | 21 ++ hphp/runtime/ext_hhvm/ext_hhvm_infotabs.cpp | 4 +- hphp/runtime/ext_hhvm/ext_hhvm_infotabs.h | 1 + hphp/system/class_map.cpp | 8 + .../ext-standard-array/array_column_basic.php | 135 +++++++++++++ .../array_column_basic.php.expectf | 188 ++++++++++++++++++ .../ext-standard-array/array_column_error.php | 36 ++++ .../array_column_error.php.expectf | 34 ++++ .../array_column_object_cast.php | 37 ++++ .../array_column_object_cast.php.expectf | 11 + .../array_column_variant.php | 17 ++ .../array_column_variant.php.expectf | 64 ++++++ 18 files changed, 720 insertions(+), 3 deletions(-) create mode 100644 hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.cpp create mode 100644 hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.h create mode 100644 hphp/test/zend/good/ext-standard-array/array_column_basic.php create mode 100644 hphp/test/zend/good/ext-standard-array/array_column_basic.php.expectf create mode 100644 hphp/test/zend/good/ext-standard-array/array_column_error.php create mode 100644 hphp/test/zend/good/ext-standard-array/array_column_error.php.expectf create mode 100644 hphp/test/zend/good/ext-standard-array/array_column_object_cast.php create mode 100644 hphp/test/zend/good/ext-standard-array/array_column_object_cast.php.expectf create mode 100644 hphp/test/zend/good/ext-standard-array/array_column_variant.php create mode 100644 hphp/test/zend/good/ext-standard-array/array_column_variant.php.expectf diff --git a/hphp/idl/array.idl.json b/hphp/idl/array.idl.json index 24219003e..e6ea98fae 100644 --- a/hphp/idl/array.idl.json +++ b/hphp/idl/array.idl.json @@ -142,6 +142,32 @@ } ] }, + { + "name": "array_column", + "desc": "Return the values from a single column in the input array, identified by the value_key and optionally indexed by the index_key", + "return": { + "type": "Variant", + "desc": "Returns the array column, or FALSE on failure" + }, + "args": [ + { + "name": "arr", + "type": "Variant", + "desc": "Source array to pull column of values from" + }, + { + "name": "val_key", + "type": "Variant", + "desc": "Key to pull values from in sub-arrays" + }, + { + "name": "idx_key", + "type": "Variant", + "desc": "Key to pull indexs from in sub-arrays", + "value": "null_variant" + } + ] + }, { "name": "array_combine", "desc": "Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.", @@ -2072,4 +2098,4 @@ ], "classes": [ ] -} \ No newline at end of file +} diff --git a/hphp/runtime/ext/ext_array.cpp b/hphp/runtime/ext/ext_array.cpp index 181bb0a07..d15981bdc 100644 --- a/hphp/runtime/ext/ext_array.cpp +++ b/hphp/runtime/ext/ext_array.cpp @@ -92,6 +92,63 @@ Variant f_array_chunk(CVarRef input, int size, getCheckedArray(input); return ArrayUtil::Chunk(arr_input, size, preserve_keys); } + +static inline bool array_column_coerce_key(Variant &key, const char *name) { + /* NULL has a special meaning for each field */ + if (key.isNull()) { + return true; + } + + /* Custom coercion rules for key types */ + if (key.isInteger() || key.isDouble()) { + key = key.toInt64(); + return true; + } else if (key.isString() || key.isObject()) { + key = key.toString(); + return true; + } else { + raise_warning("The %s key should be either a string or an integer", name); + return false; + } +} + +Variant f_array_column(CVarRef input, CVarRef val_key, + CVarRef idx_key /* = null_variant */) { + /* Be strict about array type */ + getCheckedArrayRet(input, uninit_null()); + Variant val = val_key, idx = idx_key; + if (!array_column_coerce_key(val, "column") || + !array_column_coerce_key(idx, "index")) { + return false; + } + Array ret = Array::Create(); + for(auto it = arr_input.begin(); !it.end(); it.next()) { + if (!it.second().isArray()) { + continue; + } + Array sub = it.second().toArray(); + + Variant elem; + if (val.isNull()) { + elem = sub; + } else if (sub.exists(val)) { + elem = sub[val]; + } else { + // skip subarray without named element + continue; + } + + if (idx.isNull() || !sub.exists(idx)) { + ret.append(elem); + } else if (sub[idx].isObject()) { + ret.set(sub[idx].toString(), elem); + } else { + ret.set(sub[idx], elem); + } + } + return ret; +} + Variant f_array_combine(CVarRef keys, CVarRef values) { getCheckedArray(keys); getCheckedArray(values); @@ -1077,7 +1134,7 @@ static Array::PFUNC_CMP get_cmp_func(int sort_flags, bool ascending) { class ArraySortTmp { public: - ArraySortTmp(Array& arr) : m_arr(arr) { + explicit ArraySortTmp(Array& arr) : m_arr(arr) { m_ad = arr.get()->escalateForSort(); m_ad->incRefCount(); } diff --git a/hphp/runtime/ext/ext_array.ext_hhvm.cpp b/hphp/runtime/ext/ext_array.ext_hhvm.cpp index 23487b20e..e8b9f5936 100644 --- a/hphp/runtime/ext/ext_array.ext_hhvm.cpp +++ b/hphp/runtime/ext/ext_array.ext_hhvm.cpp @@ -137,6 +137,42 @@ TypedValue* fg_array_chunk(HPHP::VM::ActRec *ar) { +/* +HPHP::Variant HPHP::f_array_column(HPHP::Variant const&, HPHP::Variant const&, HPHP::Variant const&) +_ZN4HPHP14f_array_columnERKNS_7VariantES2_S2_ + +(return value) => rax +_rv => rdi +arr => rsi +val_key => rdx +idx_key => rcx +*/ + +TypedValue* fh_array_column(TypedValue* _rv, TypedValue* arr, TypedValue* val_key, TypedValue* idx_key) asm("_ZN4HPHP14f_array_columnERKNS_7VariantES2_S2_"); + +TypedValue* fg_array_column(HPHP::VM::ActRec *ar) { + TypedValue rv; + int64_t count = ar->numArgs(); + TypedValue* args UNUSED = ((TypedValue*)ar) - 1; + if (count >= 2LL && count <= 3LL) { + fh_array_column((&(rv)), (args-0), (args-1), (count > 2) ? (args-2) : (TypedValue*)(&null_variant)); + if (rv.m_type == KindOfUninit) rv.m_type = KindOfNull; + frame_free_locals_no_this_inl(ar, 3); + memcpy(&ar->m_r, &rv, sizeof(TypedValue)); + return &ar->m_r; + } else { + throw_wrong_arguments_nr("array_column", count, 2, 3, 1); + } + rv.m_data.num = 0LL; + rv.m_type = KindOfNull; + frame_free_locals_no_this_inl(ar, 3); + memcpy(&ar->m_r, &rv, sizeof(TypedValue)); + return &ar->m_r; + return &ar->m_r; +} + + + /* HPHP::Variant HPHP::f_array_combine(HPHP::Variant const&, HPHP::Variant const&) _ZN4HPHP15f_array_combineERKNS_7VariantES2_ diff --git a/hphp/runtime/ext/ext_array.ext_hhvm.h b/hphp/runtime/ext/ext_array.ext_hhvm.h index dd8f2edbb..aa08f3366 100644 --- a/hphp/runtime/ext/ext_array.ext_hhvm.h +++ b/hphp/runtime/ext/ext_array.ext_hhvm.h @@ -41,6 +41,19 @@ preserve_keys => rcx TypedValue* fh_array_chunk(TypedValue* _rv, TypedValue* input, int size, bool preserve_keys) asm("_ZN4HPHP13f_array_chunkERKNS_7VariantEib"); +/* +HPHP::Variant HPHP::f_array_column(HPHP::Variant const&, HPHP::Variant const&, HPHP::Variant const&) +_ZN4HPHP14f_array_columnERKNS_7VariantES2_S2_ + +(return value) => rax +_rv => rdi +arr => rsi +val_key => rdx +idx_key => rcx +*/ + +TypedValue* fh_array_column(TypedValue* _rv, TypedValue* arr, TypedValue* val_key, TypedValue* idx_key) asm("_ZN4HPHP14f_array_columnERKNS_7VariantES2_S2_"); + /* HPHP::Variant HPHP::f_array_combine(HPHP::Variant const&, HPHP::Variant const&) _ZN4HPHP15f_array_combineERKNS_7VariantES2_ diff --git a/hphp/runtime/ext/ext_array.h b/hphp/runtime/ext/ext_array.h index 74d686485..294a76030 100644 --- a/hphp/runtime/ext/ext_array.h +++ b/hphp/runtime/ext/ext_array.h @@ -51,6 +51,8 @@ extern const int64_t k_UCOL_NUMERIC_COLLATION; Variant f_array_change_key_case(CVarRef input, bool upper = false); Variant f_array_chunk(CVarRef input, int size, bool preserve_keys = false); +Variant f_array_column(CVarRef arr, CVarRef val_key, + CVarRef idx_key = null_variant); Variant f_array_combine(CVarRef keys, CVarRef values); Variant f_array_count_values(CVarRef input); Variant f_array_fill_keys(CVarRef keys, CVarRef value); diff --git a/hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.cpp b/hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.cpp new file mode 100644 index 000000000..10445fe50 --- /dev/null +++ b/hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.cpp @@ -0,0 +1,29 @@ +/* + +----------------------------------------------------------------------+ + | HipHop for PHP | + +----------------------------------------------------------------------+ + | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) | + | Copyright (c) 1997-2010 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ +*/ +#include +#include +#include +#include +#include +#include +#include + +namespace HPHP { + + +} // !HPHP + diff --git a/hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.h b/hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.h new file mode 100644 index 000000000..d71cd23ac --- /dev/null +++ b/hphp/runtime/ext/hash/hash_fnv1.ext_hhvm.h @@ -0,0 +1,21 @@ +/* + +----------------------------------------------------------------------+ + | HipHop for PHP | + +----------------------------------------------------------------------+ + | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) | + | Copyright (c) 1997-2010 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ +*/ +namespace HPHP { + + +} // !HPHP + diff --git a/hphp/runtime/ext_hhvm/ext_hhvm_infotabs.cpp b/hphp/runtime/ext_hhvm/ext_hhvm_infotabs.cpp index 4ae190b16..815b5cd7a 100644 --- a/hphp/runtime/ext_hhvm/ext_hhvm_infotabs.cpp +++ b/hphp/runtime/ext_hhvm/ext_hhvm_infotabs.cpp @@ -63,6 +63,7 @@ TypedValue* fg_apd_continue(VM::ActRec *ar); TypedValue* fg_apd_echo(VM::ActRec *ar); TypedValue* fg_array_change_key_case(VM::ActRec *ar); TypedValue* fg_array_chunk(VM::ActRec *ar); +TypedValue* fg_array_column(VM::ActRec *ar); TypedValue* fg_array_combine(VM::ActRec *ar); TypedValue* fg_array_count_values(VM::ActRec *ar); TypedValue* fg_array_fill_keys(VM::ActRec *ar); @@ -3057,7 +3058,7 @@ TypedValue* tg_9XMLWriter_endDTD(VM::ActRec *ar); TypedValue* tg_9XMLWriter_flush(VM::ActRec *ar); TypedValue* tg_9XMLWriter_outputMemory(VM::ActRec *ar); -const long long hhbc_ext_funcs_count = 2204; +const long long hhbc_ext_funcs_count = 2205; const HhbcExtFuncInfo hhbc_ext_funcs[] = { { "apache_note", fg_apache_note, (void *)&fh_apache_note }, { "apache_request_headers", fg_apache_request_headers, (void *)&fh_apache_request_headers }, @@ -3099,6 +3100,7 @@ const HhbcExtFuncInfo hhbc_ext_funcs[] = { { "apd_echo", fg_apd_echo, (void *)&fh_apd_echo }, { "array_change_key_case", fg_array_change_key_case, (void *)&fh_array_change_key_case }, { "array_chunk", fg_array_chunk, (void *)&fh_array_chunk }, + { "array_column", fg_array_column, (void *)&fh_array_column }, { "array_combine", fg_array_combine, (void *)&fh_array_combine }, { "array_count_values", fg_array_count_values, (void *)&fh_array_count_values }, { "array_fill_keys", fg_array_fill_keys, (void *)&fh_array_fill_keys }, diff --git a/hphp/runtime/ext_hhvm/ext_hhvm_infotabs.h b/hphp/runtime/ext_hhvm/ext_hhvm_infotabs.h index 5a9c940df..dbf22b7a0 100644 --- a/hphp/runtime/ext_hhvm/ext_hhvm_infotabs.h +++ b/hphp/runtime/ext_hhvm/ext_hhvm_infotabs.h @@ -67,6 +67,7 @@ #include "../ext/hash/hash_adler32.ext_hhvm.h" #include "../ext/hash/hash_sha.ext_hhvm.h" #include "../ext/hash/hash_snefru.ext_hhvm.h" +#include "../ext/hash/hash_fnv1.ext_hhvm.h" #include "../ext/hash/hash_whirlpool.ext_hhvm.h" #include "../ext/hash/hash_crc32.ext_hhvm.h" #include "../ext/hash/hash_ripemd.ext_hhvm.h" diff --git a/hphp/system/class_map.cpp b/hphp/system/class_map.cpp index 14f265f96..ebc9a3a10 100644 --- a/hphp/system/class_map.cpp +++ b/hphp/system/class_map.cpp @@ -12657,6 +12657,14 @@ const char *g_class_map[] = { NULL, NULL, NULL, + (const char *)0x10006040, "array_column", "", (const char*)0, (const char*)0, + "/**\n * ( excerpt from http://php.net/manual/en/function.array-column.php )\n *\n * Return the values from a single column in the input array, identified\n * by the value_key and optionally indexed by the index_key\n *\n * @arr mixed Source array to pull column of values from\n * @val_key mixed Key to pull values from in sub-arrays\n * @idx_key mixed Key to pull indexs from in sub-arrays\n *\n * @return mixed Returns the array column, or FALSE on failure\n */", + (const char *)0xffffffff /* KindOfUnknown: $t: Variant */, (const char *)0x2000, "arr", "", (const char *)0xffffffff /* KindOfUnknown: $t: Variant */, "", (const char *)0, "", (const char *)0, NULL, + (const char *)0x2000, "val_key", "", (const char *)0xffffffff /* KindOfUnknown: $t: Variant */, "", (const char *)0, "", (const char *)0, NULL, + (const char *)0x2000, "idx_key", "", (const char *)0xffffffff /* KindOfUnknown: $t: Variant */, "N;", (const char *)2, "null", (const char *)4, NULL, + NULL, + NULL, + NULL, (const char *)0x10106040, "array_combine", "", (const char*)0, (const char*)0, "/**\n * ( excerpt from http://php.net/manual/en/function.array-combine.php )\n *\n * Creates an array by using the values from the keys array as keys and\n * the values from the values array as the corresponding values.\n *\n * @keys mixed Array of keys to be used. Illegal values for key\n * will be converted to string.\n * @values mixed Array of values to be used\n *\n * @return mixed Returns the combined array, FALSE if the number of\n * elements for each array isn't equal or if the arrays\n * are empty.\n */", (const char *)0xffffffff /* KindOfUnknown: $t: Variant */, (const char *)0x2000, "keys", "", (const char *)0xffffffff /* KindOfUnknown: $t: Variant */, "", (const char *)0, "", (const char *)0, NULL, diff --git a/hphp/test/zend/good/ext-standard-array/array_column_basic.php b/hphp/test/zend/good/ext-standard-array/array_column_basic.php new file mode 100644 index 000000000..6bc2c1cbd --- /dev/null +++ b/hphp/test/zend/good/ext-standard-array/array_column_basic.php @@ -0,0 +1,135 @@ + 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_column($records, 'first_name')); + +echo "-- id column from recordset --\n"; +var_dump(array_column($records, 'id')); + +echo "-- last_name column from recordset, keyed by value from id column --\n"; +var_dump(array_column($records, 'last_name', 'id')); + +echo "-- last_name column from recordset, keyed by value from first_name column --\n"; +var_dump(array_column($records, 'last_name', 'first_name')); + +echo "\n*** Testing multiple data types ***\n"; +$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_column($values, 'value')); +var_dump(array_column($values, 'value', 'id')); + +echo "\n*** Testing numeric column keys ***\n"; +$numericCols = array( + array('aaa', '111'), + array('bbb', '222'), + array('ccc', '333', -1 => 'ddd') +); +var_dump(array_column($numericCols, 1)); +var_dump(array_column($numericCols, 1, 0)); +var_dump(array_column($numericCols, 1, 0.123)); +var_dump(array_column($numericCols, 1, -1)); + +echo "\n*** Testing failure to find specified column ***\n"; +var_dump(array_column($numericCols, 2)); +var_dump(array_column($numericCols, 'foo')); +var_dump(array_column($numericCols, 0, 'foo')); +var_dump(array_column($numericCols, 3.14)); + +echo "\n*** Testing single dimensional array ***\n"; +$singleDimension = array('foo', 'bar', 'baz'); +var_dump(array_column($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_column($mismatchedColumns, 'c')); +var_dump(array_column($mismatchedColumns, 'c', 'a')); +var_dump(array_column($mismatchedColumns, 'a', 'd')); +var_dump(array_column($mismatchedColumns, 'a', 'e')); +var_dump(array_column($mismatchedColumns, 'b')); +var_dump(array_column($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_column($records, $f)); +var_dump(array_column($records, $f, $b)); + +echo "Done\n"; +?> diff --git a/hphp/test/zend/good/ext-standard-array/array_column_basic.php.expectf b/hphp/test/zend/good/ext-standard-array/array_column_basic.php.expectf new file mode 100644 index 000000000..f7aec28dc --- /dev/null +++ b/hphp/test/zend/good/ext-standard-array/array_column_basic.php.expectf @@ -0,0 +1,188 @@ +*** Testing array_column() : 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(%d) 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(%d) 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" +} +array(3) { + ["aaa"]=> + string(3) "111" + ["bbb"]=> + string(3) "222" + ["ccc"]=> + string(3) "333" +} +array(3) { + [0]=> + string(3) "111" + [1]=> + string(3) "222" + ["ddd"]=> + 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" +} +array(0) { +} + +*** 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 \ No newline at end of file diff --git a/hphp/test/zend/good/ext-standard-array/array_column_error.php b/hphp/test/zend/good/ext-standard-array/array_column_error.php new file mode 100644 index 000000000..cd919aebc --- /dev/null +++ b/hphp/test/zend/good/ext-standard-array/array_column_error.php @@ -0,0 +1,36 @@ + diff --git a/hphp/test/zend/good/ext-standard-array/array_column_error.php.expectf b/hphp/test/zend/good/ext-standard-array/array_column_error.php.expectf new file mode 100644 index 000000000..ad4c744f6 --- /dev/null +++ b/hphp/test/zend/good/ext-standard-array/array_column_error.php.expectf @@ -0,0 +1,34 @@ +*** Testing array_column() : error conditions *** + +-- Testing array_column() function with Zero arguments -- +HipHop Warning: %a +NULL + +-- Testing array_column() function with One argument -- +HipHop Warning: %a +NULL + +-- Testing array_column() function with string as first parameter -- +HipHop Warning: %a +NULL + +-- Testing array_column() function with int as first parameter -- +HipHop Warning: %a +NULL + +-- Testing array_column() column key parameter should be a string or an integer (testing bool) -- +HipHop Warning: %a +bool(false) + +-- Testing array_column() column key parameter should be a string or integer (testing array) -- +HipHop Warning: %a +bool(false) + +-- Testing array_column() index key parameter should be a string or an integer (testing bool) -- +HipHop Warning: %a +bool(false) + +-- Testing array_column() index key parameter should be a string or integer (testing array) -- +HipHop Warning: %a +bool(false) +Done \ No newline at end of file diff --git a/hphp/test/zend/good/ext-standard-array/array_column_object_cast.php b/hphp/test/zend/good/ext-standard-array/array_column_object_cast.php new file mode 100644 index 000000000..cd4393124 --- /dev/null +++ b/hphp/test/zend/good/ext-standard-array/array_column_object_cast.php @@ -0,0 +1,37 @@ + $value, + 'first_name' => 'John', + 'last_name' => 'XXX' + ), + array( + 'id' => 3245, + 'first_name' => 'Sally', + 'last_name' => 'Smith' + ), +); +$firstNames = array_column($records, $column_key, $index_key); +print_r($firstNames); +var_dump($column_key); +var_dump($index_key); +var_dump($value); diff --git a/hphp/test/zend/good/ext-standard-array/array_column_object_cast.php.expectf b/hphp/test/zend/good/ext-standard-array/array_column_object_cast.php.expectf new file mode 100644 index 000000000..60db4b611 --- /dev/null +++ b/hphp/test/zend/good/ext-standard-array/array_column_object_cast.php.expectf @@ -0,0 +1,11 @@ +Array +( + [2135] => John + [3245] => Sally +) +object(ColumnKeyClass)#%d (0) { +} +object(IndexKeyClass)#%d (0) { +} +object(ValueClass)#%d (0) { +} \ No newline at end of file diff --git a/hphp/test/zend/good/ext-standard-array/array_column_variant.php b/hphp/test/zend/good/ext-standard-array/array_column_variant.php new file mode 100644 index 000000000..d57ee1437 --- /dev/null +++ b/hphp/test/zend/good/ext-standard-array/array_column_variant.php @@ -0,0 +1,17 @@ + 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"; diff --git a/hphp/test/zend/good/ext-standard-array/array_column_variant.php.expectf b/hphp/test/zend/good/ext-standard-array/array_column_variant.php.expectf new file mode 100644 index 000000000..34a32e9ba --- /dev/null +++ b/hphp/test/zend/good/ext-standard-array/array_column_variant.php.expectf @@ -0,0 +1,64 @@ +-- 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 \ No newline at end of file