Joseph Lubandi
Joseph Lubandi

Reputation: 21

Extract value from multi dimensional array

I need to extract the string "Post Title" from this array. I have no idea how to reach it.

a:1:
    {   i:0;
        a:5:  
        { 
        s:4:"data";s:9:"Post Title";    
        s:7:"attribs";a:0:{}
        s:8:"xml_base";s:0:"";
        s:17:"xml_base_explicit";b:0;
        s:8:"xml_lang";s:0:"";
        }
    }

Upvotes: 1

Views: 48

Answers (1)

jh1711
jh1711

Reputation: 2328

As the comments say, you have a serialized array. But there are two potential problems with using unserialize.

You should not use unserialize on untrusted data. There's a big warning in the documentation. If you don't trust the data fully, I suggest a safer alternative like _safe_unserialize used by myBB. You can find it on github.

Second your string looks corrupted in one place. It should be s:10:"Post Title";. That means that unserialize/safe_unserialize will throw errors. To fix that, take a look at the first two answers of this question.

After you did all that, and stored the unserialized data in - let's say $arr - you can access the post title via: $array[0]['data'].

Upvotes: 1

Related Questions