嗨,我想请一些帮助。
// categories for dropdown
$this->data['dropdown_items'] = $this->category_model->get_key_value('id', 'category_name');
我的代码行返回一个数组作为key =>值对数组。因此,如果我在屏幕上转储$ this-> data [' dropdown_items'],我会得到:
array(8) {
[1] => "cinemas"
[5] => "theaters"
[7] => "night life"
[6] => "restaurants"
[4] => "food"
[2] => "night clubs"
[3] => "opera"
[8] => "misc"
}
i.e [id] => "the category name"
我想要做的是在这个数组的第一项之前添加/添加一个新项目 所以我试着用array_unshift()函数添加它:
$this->data['dropdown_items'] = array_unshift($this->data['dropdown_items'], "Please select a category");
这就是我想要的:
array(8) {
[0] => "Please select a category"
[1] => "cinemas"
[5] => "theaters"
[7] => "night life"
[6] => "restaurants"
[4] => "food"
[2] => "night clubs"
[3] => "opera"
[8] => "misc"
}
但是当我转储$ this->数据[' dropdown_items']时,我得到以下内容
int(9)
任何想法有什么不对?
答案 0 :(得分:3)
替换此
$this->data['dropdown_items'] = array_unshift($this->data['dropdown_items'], "Please select a category");
带
array_unshift($this->data['dropdown_items'], "Please select a category");
array_unshit返回数组中元素的数量,这就是您获得数组计数的原因
答案 1 :(得分:1)
array_unshift在第一个参数中接收引用数组:
array_unshift($this->data['dropdown_items'], "Please select a category");
如果将返回值赋给数组,它将被替换为数组中的元素数(int)。