Page 1 of 1

How do I turn a string to an array

Posted: Tue Oct 10, 2017 5:54 am
by adsegzy
Hello,

I have a string from my database table I want to turn into an array and get part of it. the string is as below

Code: Select all


$FromTable = '

a:11:{s:7:"form_id";s:4:"4821";s:9:"timestamp";s:10:"1501333433";s:7:"request";s:0:"";s:4:"role";s:6:"member";s:8:"_wpnonce";s:10:"23395536bf";s:16:"_wp_http_referer";s:10:"/register/";s:10:"user_login";s:11:"iCTTraining";s:10:"first_name";s:4:"Egbu";s:9:"last_name";s:0:"";s:10:"user_email";s:25:"Linksnetsystems@yahoo.com";s:5:"sr_id";s:7:"CALA038";}

'
//from the string, how do I get the value as follow
form_id = 4821
timestamp = 1501333433
......
user_email = Linksnetsystems@yahoo.com
sr_id = CALA038

//also if i need only the the value of user_email
how do i do it?

Thanks

Re: How do I turn a string to an array

Posted: Tue Oct 10, 2017 6:16 am
by Celauran
Looks like a serialized array. Try calling unserialize on it.

Re: How do I turn a string to an array

Posted: Tue Oct 10, 2017 3:25 pm
by adsegzy
Thanks Celauran,

I was able to figure it out as below

Code: Select all


$FromTable = 'a:11:{s:7:"form_id";s:4:"4821";s:9:"timestamp";s:10:"1501333433";s:7:"request";s:0:"";s:4:"role";s:6:"member";s:8:"_wpnonce";s:10:"23395536bf";s:16:"_wp_http_referer";s:10:"/register/";s:10:"user_login";s:11:"iCTTraining";s:10:"first_name";s:4:"Egbu";s:9:"last_name";s:0:"";s:10:"user_email";s:25:"Linksnetsystems@yahoo.com";s:5:"sr_id";s:7:"CALA038";}';

// Unserialize the data  
//https://www.w3resource.com/php/function-reference/unserialize.php
$var1 = unserialize($FromTable);  
//var_dump ($var1);


//https://stackoverflow.com/questions/2281526/php-how-to-get-value-from-array-if-key-is-in-a-variable
//echo "<pre>" , print_r($var1, 1) , "</pre>"; 
$sr_id = $var1['sr_id'];
$email = $var1['user_email'];
print "ID: ".$sr_id."<br/>";
print "Email: ".$email;