Salih K Salih K - 10 months ago 45
PHP Question

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(

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)

Answer Source

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