facelordgists
9/15/2015 - 5:37 AM

Custom Sort Order of Multidimentional Array

Custom Sort Order of Multidimentional Array

<?php
class CustomSortOrderMultiDimensionalArray{

  var $sort_order;
  var $sorted_array;

  function __construct( $items, $sort_order = null ){
    if( isset($sort_order) && is_array($sort_order) ){
      $this->sort_order = $sort_order;
    } else {
      return false;
    }
    $keys = array_keys($items);
    $this->sort_items( $keys );
    $keys = array_values( $keys );
    foreach ($keys as $key) {
      $sorted_array[$key] = $items[$key];
    }
    $this->sorted_array = $sorted_array;
  }

  function sort_items( array &$items ){
    uasort( $items, array( $this, 'cmp' ) );
  }

  function cmp($a, $b){
    $compare_var = $this->sort_order[$a] - $this->sort_order[$b];
    return $compare_var;
  }

}
<?php
$theFiles: Array
(
    [Addendums] => Array
        (
            [0] => Array
                (
                    [title] => Addendum No. 1
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-1-combined.pdf
                )

            [1] => Array
                (
                    [title] => Addendum No. 2
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-2.pdf
                )

            [2] => Array
                (
                    [title] => Addendum No. 3
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-3.pdf
                )

            [3] => Array
                (
                    [title] => Addendum No. 4
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-4.pdf
                )

            [4] => Array
                (
                    [title] => Addendum No. 5
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-5.pdf
                )

            [5] => Array
                (
                    [title] => AddendumNo. 6
                    [url] => http://content.jc-con.com/20150619174944/addendumno.-6.pdf
                )

            [6] => Array
                (
                    [title] => Addendum No. 7
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-7-notes.pdf
                )

            [7] => Array
                (
                    [title] => BAYOU PIPE PLANS 7.3.15
                    [url] => http://content.jc-con.com/20150619174944/bayou-pipe-plans-7.3.15.pdf
                )

            [8] => Array
                (
                    [title] => BAYOU-Pre-Eng Mtl Bldg Spec-revB
                    [url] => http://content.jc-con.com/20150619174944/bayou-pre-eng-mtl-bldg-spec-revb.pdf
                )

        )

    [Vendor Scope Sheet
] => Array
        (
            [0] => Array
                (
                    [title] => 104579-SPG-SE-1750_Earthwork and Subgrade Preparation_IFC 6 15 15
                    [url] => http://content.jc-con.com/20150619114908/104579-spg-se-1750_earthwork-and-subgrade-preparation_ifc-6-15-15.pdf
                )

            [1] => Array
                (
                    [title] => Gen Req & Scope of Work
                    [url] => http://content.jc-con.com/20150619174944/gen-req-scope-of-work.pdf
                )

        )

    [Miscellaneous] => Array
        (
            [0] => Array
                (
                    [title] => Instructions to Bid
                    [url] => http://content.jc-con.com/20150619174944/instructions-to-bid.pdf
                )

            [1] => Array
                (
                    [title] => Final Geotechincal Report Bayou Pipe
                    [url] => http://content.jc-con.com/20150619174944/final-geotechincal-report-bayou-pipe.pdf
                )

        )

    [Specifications] => Array
        (
            [0] => Array
                (
                    [title] => DIV14_CRANE_SPECIFICATIONS_june2015-1
                    [url] => http://content.jc-con.com/20150619174944/div14_crane_specifications_june2015-1.pdf
                )

        )

)




$sort_files->sorted_array : Array
(
    [Specifications] => Array
        (
            [0] => Array
                (
                    [title] => DIV14_CRANE_SPECIFICATIONS_june2015-1
                    [url] => http://content.jc-con.com/20150619174944/div14_crane_specifications_june2015-1.pdf
                )

        )

    [Vendor Scope Sheet
] => Array
        (
            [0] => Array
                (
                    [title] => 104579-SPG-SE-1750_Earthwork and Subgrade Preparation_IFC 6 15 15
                    [url] => http://content.jc-con.com/20150619114908/104579-spg-se-1750_earthwork-and-subgrade-preparation_ifc-6-15-15.pdf
                )

            [1] => Array
                (
                    [title] => Gen Req & Scope of Work
                    [url] => http://content.jc-con.com/20150619174944/gen-req-scope-of-work.pdf
                )

        )

    [Addendums] => Array
        (
            [0] => Array
                (
                    [title] => Addendum No. 1
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-1-combined.pdf
                )

            [1] => Array
                (
                    [title] => Addendum No. 2
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-2.pdf
                )

            [2] => Array
                (
                    [title] => Addendum No. 3
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-3.pdf
                )

            [3] => Array
                (
                    [title] => Addendum No. 4
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-4.pdf
                )

            [4] => Array
                (
                    [title] => Addendum No. 5
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-5.pdf
                )

            [5] => Array
                (
                    [title] => AddendumNo. 6
                    [url] => http://content.jc-con.com/20150619174944/addendumno.-6.pdf
                )

            [6] => Array
                (
                    [title] => Addendum No. 7
                    [url] => http://content.jc-con.com/20150619174944/addendum-no.-7-notes.pdf
                )

            [7] => Array
                (
                    [title] => BAYOU PIPE PLANS 7.3.15
                    [url] => http://content.jc-con.com/20150619174944/bayou-pipe-plans-7.3.15.pdf
                )

            [8] => Array
                (
                    [title] => BAYOU-Pre-Eng Mtl Bldg Spec-revB
                    [url] => http://content.jc-con.com/20150619174944/bayou-pre-eng-mtl-bldg-spec-revb.pdf
                )

        )

    [Miscellaneous] => Array
        (
            [0] => Array
                (
                    [title] => Instructions to Bid
                    [url] => http://content.jc-con.com/20150619174944/instructions-to-bid.pdf
                )

            [1] => Array
                (
                    [title] => Final Geotechincal Report Bayou Pipe
                    [url] => http://content.jc-con.com/20150619174944/final-geotechincal-report-bayou-pipe.pdf
                )

        )

)
<?php
$sort_files = new CustomSortOrderMultiDimensionalArray( 
  $theFiles, 
  array(
    'Plans' => 0, 
    'Specifications' => 1, 
    'Bid Forms' => 2, 
    'Vendor Scope Sheet
' => 3,
    'Addendums' => 4,
    'Miscellaneous' => 5,
  )
);
// $sort_files->sorted_array; <-- the goods
echo "<pre>\$sort_files->sorted_array : " . print_r($sort_files->sorted_array , true) . "</pre>";