如何以自定义排序方式对数组进行排序

时间:2017-04-20 08:15:41

标签: php arrays sorting

我的输出现在

Array
(
    [0] => Array
        (
            [post_name] => brande
            [Product Name:] => Branded
            [Company Name] => "fas"
            [Company Url] => "dasdf"
            [Blog Topic] => "asdf"
            [Blog Url-1] => "asdf"
            [Reference Url] => "asdf"
            [Keywords] => "asdf"
            [Special Instructions] => "asdf"
        )
)

我希望输出

Array
(
    [0] => Array
        (
            [post_name] => brande
            [Product Name:] => Branded
            [Reference Url] => "asdf"
            [Blog Topic] => "asdf"
            [Keywords] => "asdf"
            [Special Instructions] => "asdf"
            [Company Name] => "fas"
            [Company Url] => "dasdf"            
            [Blog Url-1] => "asdf"            
        )
)

1 个答案:

答案 0 :(得分:1)

信息:

$mCustomRowArray是包含自定义行的数组。如果您希望post_name成为最后一项,则只需更改该数组中的位置。

$mWrongArray是一组虚拟数据。

$mCustomRowArray= array(

    "post_name",
    "Product Name",
    "Reference Url",
    "Blog Topic",
    "Keywords",
    "Special Instructions",
    "Company Name",
    "Company Url",          
    "Blog Url-1",
);


$mWrongArray = array
(
    array(
            "post_name" => "post_name data",
            "Product Name" => "Product Name data",
            "Company Name" => "Company Name data",
            "Company Url" => "Company Url data",
            "Blog Topic" => "Blog Topic data",
            "Blog Url-1" => "Blog Url-1 data",
            "Reference Url" => "Reference Url data",
            "Keywords" => "Keywords data",
            "Special Instructions" => "Special Instructions data",
        ),

     array(
            "Keywords" => "Keywords data2",
            "Special Instructions" => "Special Instructions data2",
            "post_name" => "post_name data2",
            "Product Name" => "Product Name data2",
            "Company Name" => "Company Name data2",
            "Blog Url-1" => "Blog Url-1 data2",
            "Reference Url" => "Reference Url data2",
            "Company Url" => "Company Url data2",
            "Blog Topic" => "Blog Topic data2",

        ),
);


$mFinalArray = sortArrayWithCustomRowOf($mWrongArray, $mCustomRowArray);

print_r($mFinalArray);

function sortArrayWithCustomRowOf($mWrongArray, $mCustomRowArray){

    $mFinalArray = [];

    foreach($mWrongArray as $key => $current){


        foreach($mCustomRowArray as $mIndexArrayValue){

            $mTempArray[$mIndexArrayValue] = $current[$mIndexArrayValue];

        }

        $mFinalArray[] = $mTempArray;


    }


    return $mFinalArray;

}
  

输出:

Array
(
    [0] => Array
        (
            [post_name] => post_name data
            [Product Name] => Product Name data
            [Reference Url] => Reference Url data
            [Blog Topic] => Blog Topic data
            [Keywords] => Keywords data
            [Special Instructions] => Special Instructions data
            [Company Name] => Company Name data
            [Company Url] => Company Url data
            [Blog Url-1] => Blog Url-1 data
        )

    [1] => Array
        (
            [post_name] => post_name data2
            [Product Name] => Product Name data2
            [Reference Url] => Reference Url data2
            [Blog Topic] => Blog Topic data2
            [Keywords] => Keywords data2
            [Special Instructions] => Special Instructions data2
            [Company Name] => Company Name data2
            [Company Url] => Company Url data2
            [Blog Url-1] => Blog Url-1 data2
        )

)