Salih K
Salih K

Reputation: 711

How to add/append value to an array item in PHP?

I am learning a CI, I got a tutorial,Its a little old, In that trutorial it shows working without any issue, but I am getting a notice like this :: A PHP Error was encountered Severity: Notice Message: Array to string conversion

Here is the code::

$rules = array(
    'name'=>array(
        'field'=>'name', 
        'label'=>'Name', 
        'rules'=>'trim|required'
    ),
    'email'=>array(
        'field'=>'email', 
        'label'=>'Email', 
        'rules'=>'trim|required|valid_email|callback__unique_email'
    ),
    'password'=>array(
        'field'=>'password', 
        'label'=>'Password', 
        'rules'=>'trim|matches[password_confirm]'
    )
)

To add a required rule to password filed, it was used like this

$rules['password'] .= '|required';

But when I use this, not working, when I var dump, I am getting this

'password' => string 'Array|required' (length=14)

I am using PHP 5.6, Is there any thing wrong with the code, any help will be appreciated. thank you

Expected value will be like this

    array (size=4)
  'name' => 
    array (size=3)
      'field' => string 'name' (length=4)
      'label' => string 'Name' (length=4)
      'rules' => string 'trim|required' (length=13)
  'email' => 
    array (size=3)
      'field' => string 'email' (length=5)
      'label' => string 'Email' (length=5)
      'rules' => string 'trim|required|valid_email|callback__unique_email' (length=48)
  'password' => 
    array (size = 3)
    'field' => string 'password' (length=16)
        'label' => string 'Password' (length=16)
        'rules' => string 'trim|matches[confirm_password]|required' (length=22) 

Upvotes: 0

Views: 38

Answers (1)

Roman Che
Roman Che

Reputation: 137

I think you need right index specification: $rules['password']['rules'] .= '|required';

Upvotes: 1

Related Questions