0

Ok I am trying to make a page the list user meta.

http://pastebin.com/rw1t7Mgp

The part I am stuck on is

$our_children = get_user_meta ($user_id,"our_children");

it is a meta field with an array in it.

a:1:{i:0;a:2:{s:4:"Name";s:6:"Elisha";s:3:"Age";s:2:"13";}}

How do I display the data in that array?

//foreach ( $our_children as $children ) { echo '<tr><td>'. $our_children-> Name .'</td><<td>'. $our_children-> Age .'</td></tr>'; //}

Doesn't seem to work?

2 Answers 2

0

You have a few PHP issues preventing output-

  1. it appears your foreach is commented out with //.
  2. you do foreach( $our_children as $children ), but then you use $our_children instead of $children inside the foreach.
  3. $children->Age is for accessing an object's property, but what you have here is an array, so it should be $children['Age']

-

foreach ( $our_children as $children ) {
    echo '<tr><td>'. $children['Name'] .'</td><td>'. $children['Age'] .'</td></tr>';
}
1
  • Also, even if $children was an object, the space in $children-> Age and $children-> Name would cause issues.
    – Pat J
    Commented May 27, 2013 at 3:27
1

WordPress stores arrays in the database as serialized data. Once you have retrieved it, you can use php's unserialize if you're sure of the format or WordPress's maybe_unserialize.

Note that in your example you're expecting an object, that won't always be the case, it depends on how the data was stored originally.

1
  • it should be noted that get_user_meta itself uses maybe_unserialize, if you're getting serialized data back from that function there's something wrong.
    – Milo
    Commented May 27, 2013 at 2:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.