Jump to content

Recommended Posts

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) ; }

Link to comment
https://forums.phpfreaks.com/topic/307459-unserialize-trouble/
Share on other sites

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 by benanamen
Link to comment
https://forums.phpfreaks.com/topic/307459-unserialize-trouble/#findComment-1559354
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/307459-unserialize-trouble/#findComment-1559355
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/307459-unserialize-trouble/#findComment-1559385
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.