0

Hi I'd like some help please.

// categories for dropdown
$this->data['dropdown_items'] = $this->category_model->get_key_value('id', 'category_name');

This line of my code returns an array as a key=>value pair array. So if I dump $this->data['dropdown_items'] on screen I get this:

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"

What I'd like to do is to prepend/add as first item of this array a new item so I tried to add it with array_unshift() function:

$this->data['dropdown_items'] = array_unshift($this->data['dropdown_items'], "Please select a category");

This is what I was hopping to get:

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"
    }

but instead when I dump the $this->data['dropdown_items'] , I get the following

int(9)

Any ideas what's wrong?

ltdev
  • 4,037
  • 20
  • 69
  • 129

2 Answers2

3

replace this

$this->data['dropdown_items'] = array_unshift($this->data['dropdown_items'], "Please select a category");

with

array_unshift($this->data['dropdown_items'], "Please select a category");

array_unshit returns number of elements in array that`s why you are getting array count

Anand Patel
  • 3,885
  • 3
  • 17
  • 23
1

array_unshift receive a reference array in first argument :

array_unshift($this->data['dropdown_items'], "Please select a category");

If you assign the return value to your array, it will be replace by the number of elements in your array (int).

Spoke44
  • 968
  • 10
  • 24