2014-09-29 18 views
0

我不知道值執行的foreach如果我正確的措辭這個問題,但基本上我從WordPress的下面陣列結果我試圖處理:上還內置了陣列

Array 
(
    [0] => WP_User Object 
     (
      [data] => stdClass Object 
       (
        [ID] => 3 
        [user_login] => Jean Gomes 
        [user_pass] => $P$BvW6LK9/blGKLuSj.9spNPW3.v104.. 
        [user_nicename] => jean-gomes 
        [user_email] => [email protected] 
        [user_url] => 
        [user_registered] => 2014-08-28 10:56:39 
        [user_activation_key] => 
        [user_status] => 0 
        [display_name] => Jean Gomes 
       ) 

      [ID] => 3 
      [caps] => Array 
       (
        [author] => 1 
       ) 

      [cap_key] => dpa2335327_capabilities 
      [roles] => Array 
       (
        [0] => author 
       ) 

      [allcaps] => Array 
       (
        [upload_files] => 1 
        [edit_posts] => 1 
        [edit_published_posts] => 1 
        [publish_posts] => 1 
        [read] => 1 
        [level_2] => 1 
        [level_1] => 1 
        [level_0] => 1 
        [delete_posts] => 1 
        [delete_published_posts] => 1 
        [wpseo_bulk_edit] => 1 
        [author] => 1 
       ) 

      [filter] => 
     ) 

    [1] => WP_User Object 
     (
      [data] => stdClass Object 
       (
        [ID] => 4 
        [user_login] => Tammy Day 
        [user_pass] => $P$BejydpFxKDp60WruV71u0/JnJm9o.E. 
        [user_nicename] => tammy-day 
        [user_email] => [email protected] 
        [user_url] => 
        [user_registered] => 2014-08-28 10:57:23 
        [user_activation_key] => 
        [user_status] => 0 
        [display_name] => Tammy Day 
       ) 

      [ID] => 4 
      [caps] => Array 
       (
        [author] => 1 
       ) 

我想對這個ID使用這些結果執行foreach

E.g.

foreach ($authors as $author_id) { 

但我不知道如何訪問ID。我想:

foreach ($authors->ID as $author_id) { 

但很明顯這可不行,因爲有之前的數,是否有一個等價的:

foreach ($authors['X']->ID as $author_id) { 

,我可以用?

對不起,我知道這可能是一個愚蠢的問題,但我很困難。我知道我可能不得不在foreach中做一個foreach,但是我根本無法弄清楚語法。

+0

這應該工作:'的foreach($作者爲$作家){回聲$作者 - > ID; }'。 – 2014-09-29 15:44:00

回答

1
foreach (array_expression as $value) 

在每次迭代中,當前元素的值被分配給$ value並且內部數組指針前進一個(所以在下一次迭代中,您將查看下一個元素)。

閱讀有關foreach環詳情this文檔頁面:

在你的情況$值將指針的WP_User Object實例。要訪問該用戶的ID剛讀對象的屬性ID

foreach ($authors as $author) { 
    $author_id = $author->ID; 
} 
1

你不能代替試試這個的foreach:

$i=0; 
while ($i<count($authors)){ 
$author_id = $authors[$i]['data']['id']; 
//Do whatever you want here. 
$i=$i+1; 
}