2012-04-16 123 views
0

我這個讀了幾個不同的主題(like this one和一般的文檔信息有關uasort() - 我相信這是我要找的),但陣列仍然是一個有點固執(在我的用例中)。排序的對象數組

這是我有:

// This sets an array of values to a variable 
$collection_rows = get_field('collection_profiles'); 

這是print_r$collection_rows的:

Array 
(
    [0] => Array 
     (
      [collection_profile] => Array 
       (
        [0] => stdClass Object 
         (
          [ID] => 273 
          [post_author] => 1 
          [post_date] => 2012-03-26 07:53:45 
          [post_date_gmt] => 2012-03-26 13:53:45 
          [post_content] => 
          [post_title] => Profile 1 
          [post_excerpt] => 
          [post_status] => publish 
          [comment_status] => closed 
          [ping_status] => closed 
          [post_password] => 
          [post_name] => profile-1 
          [to_ping] => 
          [pinged] => 
          [post_modified] => 2012-04-12 08:07:35 
          [post_modified_gmt] => 2012-04-12 14:07:35 
          [post_content_filtered] => 
          [post_parent] => 0 
          [guid] => http://domain.com/?post_type=moulding_profiles&p=273 
          [menu_order] => 0 
          [post_type] => moulding_profiles 
          [post_mime_type] => 
          [comment_count] => 0 
          [ancestors] => Array 
           (
           ) 

          [filter] => raw 
         ) 

       ) 

      [collection_profile_note] => 1 
     ) 

    [1] => Array 
     (
      [collection_profile] => Array 
       (
        [0] => stdClass Object 
         (
          [ID] => 188 
          [post_author] => 1 
          [post_date] => 2012-02-17 15:24:24 
          [post_date_gmt] => 2012-02-17 21:24:24 
          [post_content] => 
          [post_title] => Test Profile 
          [post_excerpt] => 
          [post_status] => publish 
          [comment_status] => closed 
          [ping_status] => closed 
          [post_password] => 
          [post_name] => test-profile 
          [to_ping] => 
          [pinged] => 
          [post_modified] => 2012-02-28 14:13:32 
          [post_modified_gmt] => 2012-02-28 20:13:32 
          [post_content_filtered] => 
          [post_parent] => 0 
          [guid] => http://domain.com/?post_type=moulding_profiles&p=188 
          [menu_order] => 0 
          [post_type] => moulding_profiles 
          [post_mime_type] => 
          [comment_count] => 0 
          [ancestors] => Array 
           (
           ) 

          [filter] => raw 
         ) 

       ) 

      [collection_profile_note] => 3 
     ) 

    [2] => Array 
     (
      [collection_profile] => Array 
       (
        [0] => stdClass Object 
         (
          [ID] => 207 
          [post_author] => 1 
          [post_date] => 2012-02-23 13:35:55 
          [post_date_gmt] => 2012-02-23 19:35:55 
          [post_content] => 
          [post_title] => Casing Test Profile 
          [post_excerpt] => 
          [post_status] => publish 
          [comment_status] => closed 
          [ping_status] => closed 
          [post_password] => 
          [post_name] => casing-test-profile 
          [to_ping] => 
          [pinged] => 
          [post_modified] => 2012-02-23 13:35:55 
          [post_modified_gmt] => 2012-02-23 19:35:55 
          [post_content_filtered] => 
          [post_parent] => 0 
          [guid] => http://domain.com/?post_type=moulding_profiles&p=207 
          [menu_order] => 0 
          [post_type] => moulding_profiles 
          [post_mime_type] => 
          [comment_count] => 0 
          [ancestors] => Array 
           (
           ) 

          [filter] => raw 
         ) 

       ) 

      [collection_profile_note] => 2 
     ) 

) 

(相當doozey)

我正在尋找一個由數組鍵排序/值爲collection_profile_note。我已經試過(到這點):

$collection_rows = get_field('collection_profiles'); 
print_r($collection_rows); 
if ($collection_rows) { 
    echo '<h2>'.__('Profiles in Collection','roots').'</h2>'; 
    echo '<ul>'; 
    function cmp($a, $b) { 
     if ($a->collection_profile_note == $b->collection_profile_note) { 
      return 0; 
     } else { 
      return $a->collection_profile_note < $b->collection_profile_note ? 1 : -1; 
     } 
    } 
    usort($collection_rows, 'cmp'); 

    foreach($collection_rows as $collection_row) { 
     // Extract single post value 
     $collection_profile_field = $collection_row['collection_profile']; 
     $collection_profile_page = isset($collection_profile_field[0]) ? $collection_profile_field[0]->ID : NULL; 
     ?> 
     <li><a href="<?php echo get_permalink($collection_profile_page); ?>"><?php echo get_the_title($collection_profile_page); ?></a> <?php echo $collection_row['collection_profile_note']; ?></li> 
    <?php } 
    echo '</ul>'; 
} 

,雖然它確實改變從所顯示內容的順序沒有uasort()它沒有命令他們怎麼想 - >使用功能( 2,3,1),無功能(1,3,2)

任何幫助將不勝感激。謝謝!

+1

請從瀏覽器的「查看源代碼」中拷貝對象數組,而不是從顯示的屏幕拷貝。這將保持我們所需要的所有線條和空白,以便能夠看到那塊瘋狂的東西。 – 2012-04-16 20:28:41

+0

更新 - 我的錯 – Zach 2012-04-16 20:38:50

回答

0

嗯,你實現了排序邏輯倒退:

function cmp($a, $b) { 
    if ($a->collection_profile_note == $b->collection_profile_note) { 
     return 0; 
    } else { 
     return $a->collection_profile_note < $b->collection_profile_note ? 1 : -1; 
    } 
} 

documentation for uasort()

function cmp($a, $b) { 
    if ($a == $b) { 
     return 0; 
    } 
    return ($a < $b) ? -1 : 1; 
} 

請注意,您在使用這種邏輯:

  • 如果< b,返回1(實質上,告訴排序函數a> b)
  • 如果A> B,則返回-1(在本質上,告訴排序功能,一個< B)
+0

感謝您的輸入 - 我更新了排序邏輯 - 但實際的'foreach'語句(或'print_r()')仍然不會按照順序輸出帶有'collection_profile_note'的數組)。做了這個快速的Pastebin:http://pastebin.com/XJV9fMMr謝謝! – Zach 2012-04-17 13:17:22

+0

@Zach:您的排序邏輯在您的pastebin中仍然向後。 – FtDRbwLXw6 2012-04-17 13:45:21

+0

啊,k最後一部分現在是'return $ a-> collection_profile_note < $b-> collection_profile_note? -1:1;'但仍然沒有骰子... – Zach 2012-04-17 13:57:43

0

有必要爲您到位數組排序?這可能是更容易打出來的鍵和值你排序上,排序的,然後通過數組環(像這樣):

$sortme=array(); 
foreach($collection_rows as $key=>$profile) 
{ 
    $sortme[$key] = $profile['collection_profile_note']; 
} 

asort($sortme); 

foreach($sortme as $node=>$ignore) 
{ 
    print_r($collection_rows[$node]); 
} 

承認這一點,幾乎是翻倍的分揀時間,但也許夠你需要嗎?