如何排序依据输入数组的数组? [英] How to sort an array based on input array?

查看:120
本文介绍了如何排序依据输入数组的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法根据另一个数组排序的数组。我试过usort,uksort和uasort但我越来越行不通。计算器上的其他问题并不直接适用在这里,因为我的阵列结构是不同的。我想这个排序多维数组:

  $主要=阵列(
    [科技] =>阵列()
    [世界] =>阵列()
    [欧洲] =>阵列()

通过使用该索引数组:

  $指数=阵列(
    [0] =>欧洲
    [1] =>技术
    [2] =>世界

基本上,在这个例子中我希望欧洲来的$主阵列第一,第二技术与世界第三,因为这是他们的$指数数组中的定位。我怎么做? (请在阵列忽略小的语法错误以上)


解决方案

  $ main_sort =阵列()
的foreach($指数$关键=> $值){
    如果($主[$值])$ main_sorted [$值= $主[$值];
}

简单地通过 $指数数组循环,这些值映射到使用从 $主阵列。

I'm having trouble sorting an array according to another array. I've tried usort, uksort and uasort but I'm getting nowhere. Other questions on stackoverflow are not directly applicable here, as my array structure is different. I want to sort this multidimensional array:

$main = Array (
    [Technology] => Array ()
    [World] => Array ()
    [Europe] => Array () 
)

By using this index-array:

$index = Array (
    [0] => Europe
    [1] => Technology
    [2] => World
)

Basically, in this example I would want Europe to come first in the $main array, Technology second and World third, as this is their positioning in the $index array. How do I do that? (Please disregard little syntax errors in the arrays above)

解决方案

$main_sort = array()    
foreach ($index as $key => $value) {
    if ($main [$value]) $main_sorted [$value] = $main [$value];
}

Simply loop through the $index array and map those values to a new array using the values from the $main array.

这篇关于如何排序依据输入数组的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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