可能重复:
Able to see a variable in print_r()'s output, but not sure how to access it in code
如何从此数组中的WP_User对象中提取ID:
Array
(
[0] => Array
(
[lid] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 24
[user_login] => John Foo
[user_pass] => $P$BP0Qa4
[user_nicename] => john-foo
[user_email] => johnfoo@gmail.com
[user_url] =>
[user_registered] => 2012-10-10 11:16:24
[user_activation_key] =>
[user_status] => 0
[display_name] => John Foo
)
[ID] => 24
[caps] => Array
(
[commissie] => 1
)
[cap_key] => sa_wp_capabilities
[roles] => Array
(
[0] => commissie
)
[allcaps] => Array
(
[edit_posts] => 1
[read] => 1
[level_1] => 1
[level_0] => 1
[delete_posts] => 1
[commissie] => 1
)
[filter] =>
)
[lid_rol] => Voorzitter
)
)
我使用以下代码:
<?php
$rows = get_field('commissie_lid');
if($rows)
{
foreach($rows as $row)
{
$values = get_field('commissie_lid');
if($values)
{
echo '<ul>';
foreach($values as $value)
{
foreach ($value['lid'] as $liddata) {
echo $liddata->ID;
}
// allways good to see exactly what you are working with
echo '<pre>';
print_r($values);
echo '</pre>';
}
echo '</ul>';
}
}
}
?>
它返回用户ID,但也有很多以下注意事项:
注意:尝试在
中获取非对象的属性
有人可以帮助我吗?
答案 0 :(得分:1)
我认为问题在于:foreach ($value['lid'] as $liddata) {
此时,$value['lid']
是WP_User
对象本身,因此foreach最终会迭代WP_User
对象的所有属性。
如果您删除了该foreach,则应该能够从$value['lid']
(例如$value['lid']->ID
或$value['lid']->roles[0]
)直接访问该对象的公共属性。
希望有所帮助。