数组(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.  
  3. array_init(&fruits);
  4.  
  5. // Adding items to the array
  6. add_next_index_stringl(&fruits, SL("apple"));
  7. add_next_index_stringl(&fruits, SL("orange"));
  8. add_next_index_stringl(&fruits, SL("lemon"));
  9. add_next_index_stringl(&fruits, SL("banana"));
  10.  
  11. phalcon_array_append_str(&fruits, SL("pear"), 0);
  12.  
  13. // Get the first item in the array $fruits[0]
  14. 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.  
  4. array_init(&fruits);
  5. add_next_index_long(&fruits, 1);
  6. add_next_index_null(&fruits);
  7. add_next_index_bool(&fruits, 0);
  8. add_next_index_stringl(&fruits, SL("some string"));
  9. add_next_index_double(&fruits, 15.2);
  10. add_assoc_stringl_ex(&fruits, SL("my-index")+1, SL("another string"));
  11.  
  12. // Updating an existing index $fruits[2] = "other value";
  13. phalcon_array_update_long_string(&fruits, 2, SL("other value"), PH_SEPARATE);
  14.  
  15. // Removing an existing index unset($fruits[1]);
  16. phalcon_array_unset_long(fruits, 1);
  17.  
  18. // Removing an existing index unset($fruits["my-index"]);
  19. phalcon_array_unset_string(fruits, SL("my-index")+1);

原文: http://www.myleftstudio.com/internals/arrays.html