CyberShot Posted July 7, 2018 Share Posted July 7, 2018 I have what I think is a serialized information in my WordPress site.. Here is a partial chunk a:3:{s:7:"widgets";a:1:{i:0;a:3:{s:5:"title";s:4 I thought that if I use unserialize, it would put it into an array or something. I thought unserialize takes a string which according to the data type looks like what I would be passing after going through my foreach loop but when I try to unserialize it, it tells me i am trying to convert an array to a string. Not sure what I am doing wrong here. foreach ( $string as $option) { echo unserialize($option) ; } Quote Link to comment https://forums.phpfreaks.com/topic/307459-unserialize-trouble/ Share on other sites More sharing options...
benanamen Posted July 7, 2018 Share Posted July 7, 2018 (edited) Perhaps this example will help. <?php $array = unserialize('a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}'); foreach ($array as $k => $v){ echo "Key is $k. Value is $v\n"; } Result: Key is 1. Value is elem 1 Key is 2. Value is elem 2 Key is 3. Value is elem 3 Edited July 7, 2018 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/307459-unserialize-trouble/#findComment-1559354 Share on other sites More sharing options...
CyberShot Posted July 7, 2018 Author Share Posted July 7, 2018 That helps a little but my main problem still exists. I get my data and when I dump the variable, it says it's a string that looks like this a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3" yet when I pass that variable into the unserialize function, I get an error. I thought this is what unserialize was for. it says unserialize() expects parameter 1 to be string, array given in This is why I am confused. Quote Link to comment https://forums.phpfreaks.com/topic/307459-unserialize-trouble/#findComment-1559355 Share on other sites More sharing options...
Barand Posted July 7, 2018 Share Posted July 7, 2018 What does var_dump($variable) give you? Quote Link to comment https://forums.phpfreaks.com/topic/307459-unserialize-trouble/#findComment-1559356 Share on other sites More sharing options...
CyberShot Posted July 8, 2018 Author Share Posted July 8, 2018 when I first get the information out of WordPress, it looks like this ( this is just a short bit of it ) $theID = get_post_meta(get_the_ID()); $string = $theID['panels_data']; var_dump($string); //gives me the below line string(748) "a:3:{s:7:"widgets";a:1:{i:0;a:3:{s:5:"title";s:4:"home"; When I pass the string to unserialize function, I get an error $string = unserialize($string); echo $string; "warning" unserialize() expects parameter 1 to be string, array given in Quote Link to comment https://forums.phpfreaks.com/topic/307459-unserialize-trouble/#findComment-1559385 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.