0

it may seem to be a silly question, but i dont know why, this thing isn't working for me.

I have an exisiting array

$data = array(
                'first_name' => $first_name,
                'last_name' => $last_name,
                'email' => $email,
                'password' => $enc_password,
                'date_of_adding' => date('d/m/Y'),
                'support_timestamp' => $timestamp
            );

now i want to insert or append a new key with value into this existing array.

where the new key and its value is

'username' => $username

what i did is that

$data['username'] = $username;

but still the thing isn't working.

Can anybody tell what i am doing wrong??

3
  • 2
    It should work. Post your complete code. Or create a simple fiddle reprducing it.
    – Rikesh
    Feb 20, 2014 at 6:30
  • work fine for me . what you get on doing var_dump($data). Feb 20, 2014 at 6:30
  • Does the variable $username have a value?
    – Andy Gee
    Feb 20, 2014 at 6:42

4 Answers 4

0

try this

  $data = array(
            'first_name' => $first_name,
            'last_name' => $last_name,
            'email' => $email,
            'password' => $enc_password,
            'date_of_adding' => date('d/m/Y'),
            'support_timestamp' => $timestamp
        );

$data2 = array('username'=>$username);
$data = array_unshift($data,$data2);
0
You can do like this:

$data[]=array('username' => $username)

It will append the new array into an existing array

0

It should work using $data['username'] = $username;

Have a look at it here. Click the run icon

0

You may use

$data2 = array('username'=>$username);
$data = array_push($data,$data2);

or visit

demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.