2017-10-12 26 views
0

我目前能够从php对象数组中获取值,但是我也需要获取该关键值。密钥是可变的,所以我不能手动定义密钥。获取关键以及在foreach内的值

foreach($decodedItemMeta->meta as $meta_item){ 
    echo '<li> '; 
    echo '<strong>[key here]</strong>'; 
    echo '<p>'.stripslashes($meta_item).'</p>'; 
    echo '</li>'; 
} 

的print_r $ decodedItemMeta的:

stdClass Object ( 
    [meta] => stdClass Object ( 
     [Their name] => Name 
     [Their email] => [email protected] 
     [Gift message] => It\'s a great day for bug testing! 
    ) 
    [product] => stdClass Object () 
) 
+3

'的foreach($ decodedItemMeta->元为$键=> $ meta_item){' – ishegg

+2

[文档,文档,文档](http://www.php.net/manual/en/control-structures.foreach.php) –

+0

谢谢你的链接@MarkBaker。我在寻找正确的东西时遇到了麻烦!得到此书签供将来使用 –

回答

0

这应该为你工作

foreach($decodedItemMeta->meta as $key=> $meta_item){ 
    echo '<li> '; 
    echo '<strong>'.$key.'</strong>'; 
    echo '<p>'.stripslashes($meta_item).'</p>'; 
    echo '</li>'; 
} 
+0

完美!谢谢。 –