2017-09-02 3 views
1

私はStdClassオブジェクトの配列を持っていて、価格でソートされたいくつかの異なるCarタイプ(コンパクトカー、ミニバンなど)を含んでいます。Std Classオブジェクトの配列を特定のリスト順でソートするにはどうすればよいですか?

  • 経済カー
  • コンパクトカー
  • ミッドサイズカー
  • 標準サイズの車
  • フルサイズカー
  • プレミアム:私は、次の順序で配列をソートしたいと思います車
  • ラグジュアリーカー
  • 標準サイズ変換可能
  • ミッドサイズSUV
  • 標準SUV価格

によって並べ替え

  • ミニバン
  • 残りの車は、私はそれがこれを並べ替えることが理にかなっているかどうかわからないんだけど
  • フルサイズSUVキーの「説明」または「コード」を使用して注文します。私は価格で並べ替える方法を考え出しましたが、上記の順序で配列を並べ替える方法は不明です。どんな助けもありがとう。ありがとう!

    入力配列:

    array (
        0 => 
        stdClass::__set_state(array(
         'description' => 'Mid-Size SUV', 
         'codes' => 
         array (
         0 => 'IFAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '5', 
         'max_bags' => '4', 
         'min_bags' => '1', 
         'price' => '30.31', 
        )), 
        1 => 
        stdClass::__set_state(array(
         'description' => 'Standard-Size SUV', 
         'codes' => 
         array (
         0 => 'SFAR', 
         1 => 'RFAR', 
        ), 
         'max_people' => '7', 
         'min_people' => '5', 
         'max_bags' => '4', 
         'min_bags' => '1', 
         'price' => '35.53', 
        )), 
        2 => 
        stdClass::__set_state(array(
         'description' => 'Specialty Vehicle', 
         'codes' => 
         array (
         0 => 'XXAR', 
        ), 
         'max_people' => false, 
         'min_people' => false, 
         'max_bags' => '', 
         'min_bags' => '', 
         'price' => '36.72', 
        )), 
        3 => 
        stdClass::__set_state(array(
         'description' => 'Compact Car', 
         'codes' => 
         array (
         0 => 'CCAR', 
         1 => 'CDAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '4', 
         'max_bags' => '3', 
         'min_bags' => '1', 
         'price' => '37.21', 
        )), 
        4 => 
        stdClass::__set_state(array(
         'description' => 'Economy Car', 
         'codes' => 
         array (
         0 => 'ECAR', 
         1 => 'EDAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '4', 
         'max_bags' => '2', 
         'min_bags' => '1', 
         'price' => '37.21', 
        )), 
        5 => 
        stdClass::__set_state(array(
         'description' => 'Mid-Size Car', 
         'codes' => 
         array (
         0 => 'ICAR', 
         1 => 'IDAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '5', 
         'max_bags' => '4', 
         'min_bags' => '1', 
         'price' => '39.46', 
        )), 
        6 => 
        stdClass::__set_state(array(
         'description' => 'Standard-Size Car', 
         'codes' => 
         array (
         0 => 'SCAR', 
         1 => 'SDAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '5', 
         'max_bags' => '4', 
         'min_bags' => '1', 
         'price' => '41.77', 
        )), 
        7 => 
        stdClass::__set_state(array(
         'description' => 'Minivan', 
         'codes' => 
         array (
         0 => 'MVAR', 
        ), 
         'max_people' => '7', 
         'min_people' => '7', 
         'max_bags' => '5', 
         'min_bags' => '2', 
         'price' => '43.18', 
        )), 
        8 => 
        stdClass::__set_state(array(
         'description' => 'Full-Size Car', 
         'codes' => 
         array (
         0 => 'FCAR', 
         1 => 'FDAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '5', 
         'max_bags' => '4', 
         'min_bags' => '1', 
         'price' => '43.50', 
        )), 
        9 => 
        stdClass::__set_state(array(
         'description' => 'Compact SUV', 
         'codes' => 
         array (
         0 => 'CFAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '5', 
         'max_bags' => '3', 
         'min_bags' => '3', 
         'price' => '46.42', 
        )), 
        10 => 
        stdClass::__set_state(array(
         'description' => 'Full-Size Hybrid', 
         'codes' => 
         array (
         0 => 'FCAH', 
        ), 
         'max_people' => false, 
         'min_people' => false, 
         'max_bags' => '', 
         'min_bags' => '', 
         'price' => '48.00', 
        )), 
        11 => 
        stdClass::__set_state(array(
         'description' => 'Standard-Size Convertible', 
         'codes' => 
         array (
         0 => 'STAR', 
        ), 
         'max_people' => '4', 
         'min_people' => '4', 
         'max_bags' => '4', 
         'min_bags' => '1', 
         'price' => '48.39', 
        )), 
        12 => 
        stdClass::__set_state(array(
         'description' => 'Sports Car', 
         'codes' => 
         array (
         0 => 'XSAR', 
         1 => 'SSAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '4', 
         'max_bags' => '3', 
         'min_bags' => '1', 
         'price' => '48.72', 
        )), 
        13 => 
        stdClass::__set_state(array(
         'description' => 'Premium Car', 
         'codes' => 
         array (
         0 => 'PCAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '5', 
         'max_bags' => '4', 
         'min_bags' => '2', 
         'price' => '48.72', 
        )), 
        14 => 
        stdClass::__set_state(array(
         'description' => 'Luxury Car', 
         'codes' => 
         array (
         0 => 'LCAR', 
         1 => 'LDAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '5', 
         'max_bags' => '5', 
         'min_bags' => '1', 
         'price' => '52.16', 
        )), 
        15 => 
        stdClass::__set_state(array(
         'description' => 'Full-Size SUV', 
         'codes' => 
         array (
         0 => 'FFAR', 
        ), 
         'max_people' => '8', 
         'min_people' => '7', 
         'max_bags' => '4', 
         'min_bags' => '2', 
         'price' => '52.92', 
        )), 
        16 => 
        stdClass::__set_state(array(
         'description' => 'Full-Size Van', 
         'codes' => 
         array (
         0 => 'FVAR', 
        ), 
         'max_people' => '15', 
         'min_people' => '12', 
         'max_bags' => '5', 
         'min_bags' => '1', 
         'price' => '90.38', 
        )), 
        17 => 
        stdClass::__set_state(array(
         'description' => 'Compact Convertible', 
         'codes' => 
         array (
         0 => 'CTAR', 
        ), 
         'max_people' => '4', 
         'min_people' => '4', 
         'max_bags' => '1', 
         'min_bags' => '1', 
         'price' => '91.17', 
        )), 
        18 => 
        stdClass::__set_state(array(
         'description' => 'Premium SUV', 
         'codes' => 
         array (
         0 => 'PFAR', 
         1 => 'UFAR', 
        ), 
         'max_people' => '7', 
         'min_people' => '5', 
         'max_bags' => '5', 
         'min_bags' => '1', 
         'price' => '92.64', 
        )), 
        19 => 
        stdClass::__set_state(array(
         'description' => 'Specialty Car', 
         'codes' => 
         array (
         0 => 'XCAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '5', 
         'max_bags' => '3', 
         'min_bags' => '3', 
         'price' => '94.42', 
        )), 
        20 => 
        stdClass::__set_state(array(
         'description' => 'Mid-Size Convertible', 
         'codes' => 
         array (
         0 => 'ITAR', 
        ), 
         'max_people' => '4', 
         'min_people' => '4', 
         'max_bags' => '2', 
         'min_bags' => '2', 
         'price' => '97.98', 
        )), 
        21 => 
        stdClass::__set_state(array(
         'description' => 'Luxury SUV', 
         'codes' => 
         array (
         0 => 'LFAR', 
        ), 
         'max_people' => '5', 
         'min_people' => '5', 
         'max_bags' => '3', 
         'min_bags' => '3', 
         'price' => '113.66', 
        )), 
        22 => 
        stdClass::__set_state(array(
         'description' => 'Premium Convertible', 
         'codes' => 
         array (
         0 => 'PTAR', 
        ), 
         'max_people' => '4', 
         'min_people' => '4', 
         'max_bags' => '1', 
         'min_bags' => '1', 
         'price' => '121.15', 
        )), 
        23 => 
        stdClass::__set_state(array(
         'description' => 'Luxury Convertible', 
         'codes' => 
         array (
         0 => 'LTAR', 
        ), 
         'max_people' => '4', 
         'min_people' => '4', 
         'max_bags' => '1', 
         'min_bags' => '1', 
         'price' => '129.01', 
        )), 
        24 => 
        stdClass::__set_state(array(
         'description' => 'Premium Van', 
         'codes' => 
         array (
         0 => 'PVAR', 
        ), 
         'max_people' => '15', 
         'min_people' => '15', 
         'max_bags' => '1', 
         'min_bags' => '1', 
         'price' => '159.49', 
        )), 
        25 => 
        stdClass::__set_state(array(
         'description' => 'Specialty Convertible', 
         'codes' => 
         array (
         0 => 'XTAR', 
        ), 
         'max_people' => '4', 
         'min_people' => '4', 
         'max_bags' => '2', 
         'min_bags' => '2', 
         'price' => '307.64', 
        )), 
    ) 
    

    コード:

    usort($car_types,function($a,$b) { 
        return $a->price == $b->price ? 0 : $a->price > $b->price; 
    }); 
    
    usort($car_types, function($a, $b) use ($order) { 
        $a_set = intval(isset($order[$a->description])); 
        $b_set = intval(isset($order[$b->description])); 
    
        return $a_set === $b_set && $a_set === 1 
        ? $order[$a->description] - $order[$b->description] 
        : $b_set - $a_set; 
    }); 
    
  • +0

    'sort order'値をオブジェクトに追加し、それを使ってソートします。 – ryantxr

    +0

    これらの型に何らかの並べ替え順序を与えてから並べ替える必要があります。または、これらの名前で事前定義された配列を作成し、それらの名前に基づいて配列をソートする必要があります。 –

    答えて

    0

    発注配列を定義し、usort内で使用:

    $order = [ 
        'Economy Car' => 1, 
        'Compact Car' => 2, 
        'Mid-Size Car' => 3, 
        'Standard-Size Car' => 4, 
        'Full-Size Car' => 5, 
        'Premium Car' => 6, 
        'Luxury Car' => 7, 
        'Standard-Size Convertible' => 8, 
        'Mid-Size SUV' => 9, 
        'Standard SUV' => 10, 
        'Full-Size SUV' => 11, 
        'Minivan' => 12 
    ]; 
    
    usort($car_types, function ($a, $b) use ($order) { 
        static $notSetPriority; 
    
        $aPriority = isset($order[$a->description]) 
         ? $order[$a->description] 
         : $notSetPriority ?? ($notSetPriority = count($order) + 1); 
        $bPriority = isset($order[$b->description]) 
         ? $order[$b->description] 
         : $notSetPriority; 
    
        if (($comparison = $aPriority - $bPriority) === 0) { 
         return floatval($a->price) - floatval($b->price); 
        } 
    
        return $comparison; 
    }); 
    

    説明のためのオーダー重量がない場合には設定した後、我々はそれに最も多くのwの重みを与える8プラス1。

    usortは、そのように結果を予測することが難しいため、複数回適用しないでください。私は両方のソート基準を一つにまとめました。

    +0

    ありがとうございました!これは完璧です。 1つのこと:$オーダーで定義されていない残りの車(Mid-Size Specialty Vehicleなど)については、残りの車をどのように価格で並べ替えることができますか?たとえば、ミニバンが中型特殊車両、コンパクトSUV、標準サイズピックアップ、特殊車両などに来るとしたら... –

    +0

    @MichaelAlterio、まず価格で配列を並べ替えます(質問で指定した 'usort'を適用します) )、次に説明でソートします(答えから 'usort')。これはトリックを行う必要があります。 – sevavietl

    +0

    元の投稿を価格(最低から最高)の入力配列でソートして更新しました。私が価格でソートし、上記の注文を使用すると、注文後に最も低い価格から最も高い価格にソートされません。何が起きているのか? –

    関連する問題