ObstacleCoder
ObstacleCoder

Reputation: 170

How can I output specific elements in this php array?

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.

Upvotes: 0

Views: 115

Answers (2)

moonwave99
moonwave99

Reputation: 22817

You have to unserialize your array:

$my_wake_playlist = unserialize($my_wake_playlist);

and you'll have your PHP array back.

Upvotes: 3

claustrofob
claustrofob

Reputation: 4984

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.

Upvotes: 2

Related Questions