Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,7 @@ PHP_FUNCTION(extract)
}
/* }}} */

static void php_compact_var(HashTable *eg_active_symbol_table, zval *return_value, zval *entry) /* {{{ */
static void php_compact_var(HashTable *eg_active_symbol_table, zval *return_value, zval *entry, uint32_t pos) /* {{{ */
{
zval *value_ptr, data;

Expand Down Expand Up @@ -2475,11 +2475,14 @@ static void php_compact_var(HashTable *eg_active_symbol_table, zval *return_valu
Z_PROTECT_RECURSION_P(entry);
}
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(entry), value_ptr) {
php_compact_var(eg_active_symbol_table, return_value, value_ptr);
php_compact_var(eg_active_symbol_table, return_value, value_ptr, pos);
} ZEND_HASH_FOREACH_END();
if (Z_REFCOUNTED_P(entry)) {
Z_UNPROTECT_RECURSION_P(entry);
}
} else {
php_error_docref(NULL, E_WARNING, "Argument #%d must be string or array of strings, %s given", pos, zend_zval_type_name(entry));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message is a bit imprecise when it comes to values in arrays, but I think it's good enough to go on...

return;
}
}
/* }}} */
Expand Down Expand Up @@ -2512,7 +2515,7 @@ PHP_FUNCTION(compact)
}

for (i = 0; i < num_args; i++) {
php_compact_var(symbol_table, return_value, &args[i]);
php_compact_var(symbol_table, return_value, &args[i], i + 1);
}
}
/* }}} */
Expand Down
10 changes: 10 additions & 0 deletions ext/standard/tests/array/compact.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ $location_vars = array("c\\u0327ity", "state");

$result = compact("event", $location_vars);
var_dump($result);

$result = compact(true);
$foo = 'bar';
$bar = 'baz';
$result = compact($foo, [42]);

?>
--EXPECTF--
Warning: compact(): Undefined variable $c\u0327ity in %s on line %d
Expand All @@ -20,3 +26,7 @@ array(2) {
["state"]=>
string(2) "CA"
}

Warning: compact(): Argument #1 must be string or array of strings, bool given in %s on line %d

Warning: compact(): Argument #2 must be string or array of strings, int given in %s on line %d
15 changes: 1 addition & 14 deletions ext/standard/tests/array/compact_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ $f="string";
var_dump (compact(array("a", "b", "c", "d", "e", "f")));
// simple parameter test
var_dump (compact("a", "b", "c", "d", "e", "f"));
var_dump (compact(array("keyval"=>"a", "b"=>"b", "c"=>1)));

// cases which should not yield any output.
var_dump (compact(array(10, 0.3, true, array(20), NULL)));
var_dump (compact(10, 0.3, true, array(20), NULL));
var_dump (compact(array("g")));
var_dump (compact(array("keyval"=>"a", "b"=>"b")));

echo "Done";
?>
Expand Down Expand Up @@ -70,12 +65,4 @@ array(2) {
["b"]=>
float(0.2)
}
array(0) {
}
array(0) {
}

Warning: compact(): Undefined variable $g in %s on line %d
array(0) {
}
Done