Jump to content

unserialize trouble


CyberShot

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
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

Link to comment
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
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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.