Nisanth
Nisanth

Reputation: 333

How to serialize and unserialize an array with correct format

I have an like below:

$permis =array(
 'employee' => array(
      'myprofile' => array(
        'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0'
        ),
        'view emp' => array(
        'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0', 'notes' => '0', 'onboard' => '0', 'offboard' => '0', 'charts' => '0'
        )
    )
);

If i am serialize this means it will look like below:

a:1:{s:8:"employee";a:2:{s:9:"myprofile";a:9:{s:7:"default";s:1:"0";s:8:"personal";s:1:"0";s:3:"job";s:1:"0";s:5:"leave";s:1:"0";s:10:"permission";s:1:"0";s:17:"bonus & commision";s:1:"0";s:8:"document";s:1:"0";s:17:"emergency contact";s:1:"0";s:8:"benifits";s:1:"0";}s:8:"view emp";a:13:{s:7:"default";s:1:"0";s:8:"personal";s:1:"0";s:3:"job";s:1:"0";s:5:"leave";s:1:"0";s:10:"permission";s:1:"0";s:17:"bonus & commision";s:1:"0";s:8:"document";s:1:"0";s:17:"emergency contact";s:1:"0";s:8:"benifits";s:1:"0";s:5:"notes";s:1:"0";s:7:"onboard";s:1:"0";s:8:"offboard";s:1:"0";s:6:"charts";s:1:"0";}}}

an if i unserialize means it will look like below:

Array ( [employee] => Array ( [myprofile] => Array ( [default] => 0 [personal] => 0 [job] => 0 [leave] => 0 [permission] => 0 [bonus & commision] => 0 [document] => 0 [emergency contact] => 0 [benifits] => 0 ) [view emp] => Array ( [default] => 0 [personal] => 0 [job] => 0 [leave] => 0 [permission] => 0 [bonus & commision] => 0 [document] => 0 [emergency contact] => 0 [benifits] => 0 [notes] => 0 [onboard] => 0 [offboard] => 0 [charts] => 0 ) ) )

What i need is in my unserialize array will br like below:

array(
 'employee' => array(
      'myprofile' => array(
        'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0'
        ),
        'view emp' => array(
        'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0', 'notes' => '0', 'onboard' => '0', 'offboard' => '0', 'charts' => '0'
        )
    )
)

How to Do that in right way.

Please Help Me

Upvotes: 2

Views: 248

Answers (2)

Buga Dániel
Buga Dániel

Reputation: 830

The array is unserialized correctly, your problem is that you are trying to access the elements in an incorrect way.

When you write $val2['default'], it looks for the 'default' key in the top level, where you only have 'employee'.

In order to access the 'default' for either employee, you'll need to index it level by level, like this:

$val2['employee']['myprofile']['default']

Upvotes: 1

Wim Pruiksma
Wim Pruiksma

Reputation: 598

This is your correct array. If you want to print the array with structure use

<?php 
echo "<pre>",print_r($array),"</pre>";
?>

Read the manual http://php.net/manual/en/function.serialize.php as it will be explained. Here is an easy online generator where you can test if your array is valid for sirialize http://php.fnlist.com/php/serialize

EDIT

 <?php 

    $permis =array(
     'employee' => array(
          'myprofile' => array(
            'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0'
            ),
            'view emp' => array(
            'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0', 'notes' => '0', 'onboard' => '0', 'offboard' => '0', 'charts' => '0'
            )
        )
    );

    $serialized = serialize($permis);
    echo "<pre>",print_r($serialized),"</pre>"; //echo serialized array

will output

a:1:{s:8:"employee";a:2:{s:9:"myprofile";a:9:{s:7:"default";s:1:"0";s:8:"personal";s:1:"0";s:3:"job";s:1:"0";s:5:"leave";s:1:"0";s:10:"permission";s:1:"0";s:17:"bonus & commision";s:1:"0";s:8:"document";s:1:"0";s:17:"emergency contact";s:1:"0";s:8:"benifits";s:1:"0";}s:8:"view emp";a:13:{s:7:"default";s:1:"0";s:8:"personal";s:1:"0";s:3:"job";s:1:"0";s:5:"leave";s:1:"0";s:10:"permission";s:1:"0";s:17:"bonus & commision";s:1:"0";s:8:"document";s:1:"0";s:17:"emergency contact";s:1:"0";s:8:"benifits";s:1:"0";s:5:"notes";s:1:"0";s:7:"onboard";s:1:"0";s:8:"offboard";s:1:"0";s:6:"charts";s:1:"0";}}}

 $unserialized = unserialize($serialized);
    echo "<pre>",print_r($unserialized),"</pre>"; //echo unserialized array
    ?>

will output

array( 'employee' => array( 'myprofile' => array( 'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0' ), 'view emp' => array( 'default' => '0', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0', 'notes' => '0', 'onboard' => '0', 'offboard' => '0', 'charts' => '0' ) ) );

I checked this twice. The array is correct and the unserialize is also correct. http://php.fnlist.com/php/serialize If you want to echo a key of the array use a foreach or something like this

<?php
echo $unserilized['employee']['myprofile']['personal'];

?> will output

0 as it is the value of the key

Upvotes: 1

Related Questions