How to save value in array

Hi guys,

how to save value in array which later will save inside sql db?? Please give me a sample to refer. Thx.

For example, how to save a user have two company_id inside user db?

In user table


id     name       company_id

1          Tom              1,2

2          James            3,4

Is not correct saving multiple data in a single field. But if you want you ca do it. To save you can do:


$user->company_id = '1,2';

But for me, is not a good idea.

Hi Sensorario,

My purpose to save in array type because i don’t want to duplicate extra info at user table. so i using array to save value in db.

In user table


id     name       company_id

1          Tom              1;2

2          James            3;4

In company table


id     name       user_id

1          A              1

2          B              1

3          C              2 

4          D              2            

is there a better way or suggestion to show me??

you can use simple array, and serialize -> save it into the database

Hi dckurushin and others,

can give me more specific example and link? i don’t know how to start it by save a array in db. Currently it always overwrite the previous company_id info in User table when i save new record. How to save a record if user is allow have more than 1 record in company_id User table.

User table


id     name       company_id

1          Tom              1                //currently it always overwrite when save new record using Tom name

2          James            3

User table


id     name       company_id

1          Tom              1;2                //the array record that wanted to save in db

2          James            3

Company table


id     name       user_id

1          A              1

2          B              1

3          C              2 

4          D              2     

The usual approach is to use:

user table

id name

company table

id name

user_company table

user_id company_id

If you still want to store an array, just use


$userModel->company = serialize($yourArray).

Please read about serialize function if you’re not familiar with it.