Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More native functions for arrays #945

Merged
merged 1 commit into from Jul 27, 2013
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 62 additions & 0 deletions ext/kernel/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,38 @@ void phalcon_array_unshift(zval *arr, zval *arg)
}
}

void phalcon_array_keys(zval *return_value, zval *arr) {

if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
HashPosition pos;
zval **entry, *new_val;
char *skey;
uint skey_len;
ulong nkey;

array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(arr)));

zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void**)&entry, &pos) == SUCCESS) {
MAKE_STD_ZVAL(new_val);

switch (zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &skey, &skey_len, &nkey, 1, &pos)) {
case HASH_KEY_IS_STRING:
ZVAL_STRINGL(new_val, skey, skey_len - 1, 0);
zend_hash_next_index_insert(Z_ARRVAL_P(arr), &new_val, sizeof(zval*), NULL);
break;

case HASH_KEY_IS_LONG:
ZVAL_LONG(new_val, nkey);
zend_hash_next_index_insert(Z_ARRVAL_P(arr), &new_val, sizeof(zval*), NULL);
break;
}

zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
}
}
}

void phalcon_array_values(zval *return_value, zval *arr)
{
if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
Expand Down Expand Up @@ -1452,3 +1484,33 @@ int phalcon_array_key_exists(zval *arr, zval *key TSRMLS_DC)

return 0;
}

int phalcon_array_is_associative(zval *arr) {

if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
HashPosition pos;
zval **entry;
char *skey;
uint skey_len;
ulong nkey;
ulong expected = 0;

zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void**)&entry, &pos) == SUCCESS) {

if (HASH_KEY_IS_LONG == zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &skey, &skey_len, &nkey, 1, &pos)) {
if (expected != nkey) {
return 1;
}
}
else {
return 1;
}

++expected;
zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
}
}

return 0;
}
2 changes: 2 additions & 0 deletions ext/kernel/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,7 @@ extern void phalcon_fast_array_merge(zval *return_value, zval **array1, zval **a
extern void phalcon_array_merge_recursive_n(zval **a1, zval *a2);

extern void phalcon_array_unshift(zval *arr, zval *arg);
extern void phalcon_array_keys(zval *return_value, zval *arr);
extern void phalcon_array_values(zval *return_value, zval *arr);
extern int phalcon_array_key_exists(zval *arr, zval *key TSRMLS_DC);
extern int phalcon_array_is_associative(zval *arr);