Othman
Othman

Reputation: 3018

Explode after Implode

I have this array, and I've tried to return it like the same way before.

Example:

        $this->permission_array = array(
                                    'orders'                => 1,
                                    'orders_Status'         => 0,
                                    'schools'               => 0,
                                    'accommodation'         => 0,
                                    'users'                 => 1,
                                    'news'                  => 0,
                                    'blocks'                => 1,
                                    'settings'              => 0,
                                    'videos'                => 1,
                                    );

after implode it and insert to to database it became like this.

USER_ID     USER_NAME           USER_PERMISSION
---------------------------------------------------
  1302       Othman             1,0,0,0,1,0,1,0,1

and now if I used explode it return as an array but the keys are not the same. how I can transfer these values to the same array not like this array:

[0] => 1
[1] => 0
[2] => 0
[3] => 0
[4] => 1
[5] => 0
[6] => 1
[7] => 0
[8] => 1  

Is there anyway to change the last array to the first array again? Thank's in advance.

My Desision is to change the database field and create a new table. Thank you guys

Upvotes: 1

Views: 397

Answers (6)

vpbot
vpbot

Reputation: 2487

Tho not implied, you need Normalization, store the value as a one whole string in double quotes "" in case it will affect your db eliminating the need to restructure your db.

Upvotes: 0

user1400378
user1400378

Reputation: 1

simple ...

    $a=explode(',',$permitions_from_database);
    $names=array('orders','orders_Status','etc','etc')

    $i=0;
    foreach($names as $name)
    {
    $permission_array[$name]=$a[$i];
    $i++;
    }

But i think you should put the data on different columns, its better to query the database for who has permissions for something rather than bringing all the data to php and then process it.

I hope that it helps :)

Upvotes: 0

Thunder Rabbit
Thunder Rabbit

Reputation: 5449

Create a table of user_permissions:

permission_id    permission_name

Then create a table of users_2_permissions

user_id          permission_id

When you want to restore the array, you'll do a JOIN on users, users_2_permissions and user_permissions.

Upvotes: 1

xdazz
xdazz

Reputation: 160853

The implode function only cares about the values.

Instead of use implode and explode, you could use json_encode and json_decode to use json as your format to save, that will remain your key infomations.

But the real problem is your db design.

Upvotes: 1

Mikhail
Mikhail

Reputation: 671

You can create another table for this, for example USER_PERMISSIONS (id, user_id, orders, orders_Status, ...) where id is a primary key, user_id is a foreign key to USERS table, and all the other fields contain values for respective permissions. This approach can add a bit more complexity to your queries, but it's still better than serializing arrat into string.

Upvotes: 0

slash197
slash197

Reputation: 9034

Use serialize() to store arrays in the database.

Upvotes: 0

Related Questions