0

I am using Gravity Forms for WordPress and one of the form elements is a List item that can have multiple entries. I have a page on the website that I am building that will output the data from this item onto the page but I am having trouble accessing the actual items from the array.

This is the code I am using to retrieve the array:

$user_id = $current_user->ID;
$key = 'my_playlist';
$single = false;

$my_playlist = get_user_meta( $user_id, $key, $single );
print_r(array_values($my_playlist));

The output I get from this looks like this:

Array ( [0] => a:2:{i:0;a:2:{s:10:"Song Title";s:15:"test song title";s:11:"Song Artist";s:16:"test song artist";}i:1;a:2:{s:10:"Song Title";s:11:"test song 2";s:11:"Song Artist";s:13:"test artist 2";}} )

So far I have tried to access the elements like this:

foreach($my_playlist as $item){
    echo 'Item: ' . $item[0] . '<br />';
}

but it only outputs Item: a

I would like to output it like:

Song Title: test song title Song Artist: test song artist

Song Title: test song 2 Song Artist: test artist 2

How do I access each element in this array? Also, what type of array is this? It might help me track down how to access the required items.

3

2 Answers 2

3

You have to unserialize your array:

$my_wake_playlist = unserialize($my_wake_playlist);

and you'll have your PHP array back.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this has saved me heaps of time. To get it to serialize the array in my case I had to use my_wake_playlist = unserialize($my_wake_playlist[0]);
2

This string:

a:2:{i:0;a:2:{s:10:"Song Title";s:15:"test song title";s:11:"Song Artist";s:16:"test song artist";}i:1;a:2:{s:10:"Song Title";s:11:"test song 2";s:11:"Song Artist";s:13:"test artist 2"

is serialized. Use unserialize function to decode it.

1 Comment

Thanks for your answer. It pointed me in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.