Nisanth
Nisanth

Reputation: 333

how to get a value of an array while unserialize the array

I have an serialized array like below:

a:22:{s:18:"'myprofiledefault'";s:1:"2";s:19:"'myprofilepersonal'";s:1:"0";s:14:"'myprofilejob'";s:1:"0";s:16:"'myprofileleave'";s:1:"0";s:21:"'myprofilepermission'";s:1:"0";s:28:"'myprofilebonus & commision'";s:1:"0";s:19:"'myprofiledocument'";s:1:"0";s:28:"'myprofileemergency contact'";s:1:"0";s:19:"'myprofilebenifits'";s:1:"0";s:17:"'view empdefault'";s:1:"0";s:18:"'view emppersonal'";s:1:"0";s:13:"'view empjob'";s:1:"0";s:15:"'view empleave'";s:1:"0";s:20:"'view emppermission'";s:1:"0";s:27:"'view empbonus & commision'";s:1:"0";s:18:"'view empdocument'";s:1:"0";s:27:"'view empemergency contact'";s:1:"0";s:18:"'view empbenifits'";s:1:"0";s:15:"'view empnotes'";s:1:"0";s:17:"'view emponboard'";s:1:"0";s:18:"'view empoffboard'";s:1:"0";s:16:"'view empcharts'";s:1:"0";}

If i unserialized this and print means it will be look like below:

Array(['myprofiledefault'] => 2
['myprofilepersonal'] => 0
['myprofilejob'] => 0
['myprofileleave'] => 0
['myprofilepermission'] => 0
['myprofilebonus & commision'] => 0
['myprofiledocument'] => 0
['myprofileemergency contact'] => 0
['myprofilebenifits'] => 0
['view empdefault'] => 0
['view emppersonal'] => 0
['view empjob'] => 0
['view empleave'] => 0
['view emppermission'] => 0
['view empbonus & commision'] => 0
['view empdocument'] => 0
['view empemergency contact'] => 0
['view empbenifits'] => 0
['view empnotes'] => 0
['view emponboard'] => 0
['view empoffboard'] => 0
['view empcharts'] => 0)

My question is that i want to get the key values individually.

I am trying this one

echo $ret['myprofilepersonal'];

but this is not working showing an error undefined index. Please How to get this

Upvotes: 0

Views: 58

Answers (1)

Stavros
Stavros

Reputation: 264

If we suppose that you unserialize the array you posted like so:

$ret = unserialize(...);

Then you need to access the values using sth like this:

echo $ret["'myprofiledefault'"];
// or by escaping single quote:
echo $ret['\'myprofiledefault\''];

because as I see, every key is quoted

Upvotes: 4

Related Questions