按给定的键名重新排序数组

时间:2016-11-27 03:25:03

标签: php arrays

我有一个像这样的PHP数组:

Array
(
    [stripe] => Array
        (
            [title] => Stripe
        )

    [paypal_standard] => Array
        (
            [title] => PayPal Standard
        )
)

鉴于键'paypal_standard',我如何重新排序数组以便结果:

Array
(
    [paypal_standard] => Array
        (
            [title] => PayPal Standard
        )
    [stripe] => Array
        (
            [title] => Stripe
        )
)

也许我错过了它,但是有没有这样做的PHP函数?

编辑:我无法按字母顺序排序。

1 个答案:

答案 0 :(得分:1)

试试这个:

$response = array(
    'stripe' => array('title' => 'Stripe'),
    'paypal_standard' => array('title' => 'Paypal standard'),
);

$paypal = $response['paypal_standard']; // Filter out the value corresponding to paypal_standard

unset($response['paypal_standard']); // Since filtered out, remove paypal_standard key from array

$response = array('paypal_standard' => $paypal) + $response; // Format the array by adding key value pair for paypal_standard in the beginning

输出:

Array
(
 [paypal_standard] => Array
    (
        [title] => Paypal standard
    )

 [stripe] => Array
    (
        [title] => Stripe
    )

)