数组(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)
- zval fruits = {}, first_item = {};
- array_init(&fruits);
- // Adding items to the array
- add_next_index_stringl(&fruits, SL("apple"));
- add_next_index_stringl(&fruits, SL("orange"));
- add_next_index_stringl(&fruits, SL("lemon"));
- add_next_index_stringl(&fruits, SL("banana"));
- phalcon_array_append_str(&fruits, SL("pear"), 0);
- // Get the first item in the array $fruits[0]
- phalcon_array_fetch_long(&first_item, &fruits, 0, PH_NOISY_CC);
Mixing both string and number indexes:
- // Let's create the following array using the Phalcon API
- // $fruits = array(1, null, false, "some string", 15.20, "my-index" => "another string");
- array_init(&fruits);
- add_next_index_long(&fruits, 1);
- add_next_index_null(&fruits);
- add_next_index_bool(&fruits, 0);
- add_next_index_stringl(&fruits, SL("some string"));
- add_next_index_double(&fruits, 15.2);
- add_assoc_stringl_ex(&fruits, SL("my-index")+1, SL("another string"));
- // Updating an existing index $fruits[2] = "other value";
- phalcon_array_update_long_string(&fruits, 2, SL("other value"), PH_SEPARATE);
- // Removing an existing index unset($fruits[1]);
- phalcon_array_unset_long(fruits, 1);
- // Removing an existing index unset($fruits["my-index"]);
- phalcon_array_unset_string(fruits, SL("my-index")+1);