2012-04-16 18 views
0

私はこれについて(like this oneと一般的なドキュメントの情報であるuasort() - 私が探していると信じている)いくつかの異なるトピックを読んだことがありますが、配列はまだ少し頑固です私のユースケースでは)。オブジェクトの配列を並べ替える

は、ここで私が持っているものです。

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

これは$collection_rowsprint_rです:

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()せずにそれが表示される内容から、順序を変更しながら、それは私が好きなそれらをどのように注文していません - >機能付き( (1,2,3,1)、機能なし(1,3,2)

ご協力いただければ幸いです。ありがとう!

+1

表示されている画面からではなく、ブラウザの「表示元」からオブジェクト配列をコピーしてください。それは、狂気のブロックに何があるのか​​を見るために必要なすべての改行と空白を維持します。 –

+0

更新 - 私の悪い – Zach

答えて

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、return 1(本質的に、a> bであるソート関数を伝える)
  • あなたの場所で配列をソートするために> B、-1を返します(本質的には、その< Bソート機能を伝える)
+0

入力のおかげで - 私はソートロジックを更新しました - 実際の 'foreach'ステートメント(または' print_r() 'について)' collection_profile_note'を使って配列を出力していない(昇順)。これの素早いPastebinを作った:http://pastebin.com/XJV9fMMrありがとう! – Zach

+0

@ Zach:あなたのソートロジックはまだあなたのペーストビンの中にあります。 – FtDRbwLXw6

+0

ああ、最後の部分は 'return $ a-> collection_profile_noteになりました< $b-> collection_profile_note? -1:1;でも、ダイスはありません... – Zach

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]); 
} 

が、これはほとんどのソートの時間を倍ADIは、多分十分にあなたのためにニーズ?

関連する問題