数组(Working with Arrays)

尽管 Zend API 提供了一些函数来处理数组,但 Phalcon API 中在此基础上封装了更多的函数(kernel/array.h),通过传递标志位(flag)自动完引用计数的处理。

标志位(flag)

标志位值 描述
PH_NOISY 值不存在时发出警告
PH_READONLY 不增加引用计数
PH_COPY 增加引用计数

常用函数列表

函数名
phalcon_array_isset
phalcon_array_fetch
phalcon_array_isset_fetch
phalcon_array_update
phalcon_array_append
phalcon_array_unset

一维数组(One dimension Arrays) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  1. zval fruits = {}, first_item = {};
  2. array_init(&fruits);
  3. // Adding items to the array
  4. add_next_index_stringl(&fruits, SL("apple"));
  5. add_next_index_stringl(&fruits, SL("orange"));
  6. add_next_index_stringl(&fruits, SL("lemon"));
  7. add_next_index_stringl(&fruits, SL("banana"));
  8. phalcon_array_append_str(&fruits, SL("pear"), 0);
  9. // Get the first item in the array $fruits[0]
  10. phalcon_array_fetch_long(&first_item, &fruits, 0, PH_NOISY_CC);

Mixing both string and number indexes:

  1. // Let's create the following array using the Phalcon API
  2. // $fruits = array(1, null, false, "some string", 15.20, "my-index" => "another string");
  3. array_init(&fruits);
  4. add_next_index_long(&fruits, 1);
  5. add_next_index_null(&fruits);
  6. add_next_index_bool(&fruits, 0);
  7. add_next_index_stringl(&fruits, SL("some string"));
  8. add_next_index_double(&fruits, 15.2);
  9. add_assoc_stringl_ex(&fruits, SL("my-index")+1, SL("another string"));
  10. // Updating an existing index $fruits[2] = "other value";
  11. phalcon_array_update_long_string(&fruits, 2, SL("other value"), PH_SEPARATE);
  12. // Removing an existing index unset($fruits[1]);
  13. phalcon_array_unset_long(fruits, 1);
  14. // Removing an existing index unset($fruits["my-index"]);
  15. phalcon_array_unset_string(fruits, SL("my-index")+1);