使用php按键对数组值进行排序 [英] sort array values by key using php

查看:138
本文介绍了使用php按键对数组值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下格式的PHP数组

i have an PHP array in the following format,

Array
(
    [0] => Array
        (
            [40] => 2
            [80] => 1
            [20] => 0
            [60] => 0
            [100] => 0
        )

    [1] => Array
        (
            [60] => 2
            [80] => 1
            [20] => 0
            [40] => 0
            [100] => 0
        )
)

预期输出为(20 => 0,40 => 0,60 => 2,80 => 1等) 我尝试使用array_values和array_combine,但没有运气.

Expected output is (20=>0,40=>0,60=>2,80=>1 etc) i tried using array_values and array_combine, but no luck.

如何使用PHP按键对数组进行排序.

How do i sort array by key using PHP.

推荐答案

使用array_combine(

Use array_combine (http://php.net/manual/en/function.array-combine.php) to combine the arrays and then use ksort (http://php.net/manual/en/function.ksort.php) to sort the keys.

这篇关于使用php按键对数组值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆