排序多维数组由子阵列键值 [英] Sort Multidimensional arrays by sub array key value

查看:87
本文介绍了排序多维数组由子阵列键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有类似的问题,并发布这个答案,但他们都不完全匹配我的数组的结构,因此,如果道歉我错过了什么。这是道$ P $ WPDB PSS类生成一个数组:

 阵列(
[0] =>阵列([meta_id] => 37850 [POST_ID] => 5548 [meta_key] =>项目#[meta_value] => 66002)
[1] =>阵列([meta_id] => 37851 [POST_ID] => 5548 [meta_key] =>六角尺寸[meta_value] => .051)
[2] =>阵列([meta_id] => 37852 [POST_ID] => 5548 [meta_key] =>对边[meta_value] => 0.051)
[3] =>阵列([meta_id] => 37853 [POST_ID] => 5548 [meta_key] =>类型[meta_value] =>六角)
[4] =>阵列([meta_id] => 37854 [POST_ID] => 5548 [meta_key] =>柄[meta_value] => .315))阵列(
[0] =>阵列([meta_id] => 37910 [POST_ID] => 5553 [meta_key] =>项目#[meta_value] => 66008)
[1] =>阵列([meta_id] => 37911 [POST_ID] => 5553 [meta_key] =>六角尺寸[meta_value] => 1/8)
[2] =>阵列([meta_id] => 37912 [POST_ID] => 5553 [meta_key] =>对边[meta_value] => 0.127)
[3] =>阵列([meta_id] => 37913 [POST_ID] => 5553 [meta_key] =>类型[meta_value] =>六角)
[4] =>阵列([meta_id] => 37914 [POST_ID] => 5553 [meta_key] =>柄[meta_value] => .315))阵列(
[0] =>阵列([meta_id] => 37862 [POST_ID] => 5549 [meta_key] =>项目#[meta_value] => 66004)
[1] =>阵列([meta_id] => 37863 [POST_ID] => 5549 [meta_key] =>六角尺寸[meta_value] => 1/16)
[2] =>阵列([meta_id] => 37864 [POST_ID] => 5549 [meta_key] =>对边[meta_value] => 0.063)
[3] =>阵列([meta_id] => 37865 [POST_ID] => 5549 [meta_key] =>类型[meta_value] =>六角)
[4] =>阵列([meta_id] => 37866 [POST_ID] => 5549 [meta_key] =>柄[meta_value] => .315))阵列(
[0] =>阵列([meta_id] => 37886 [POST_ID] => 5551 [meta_key] =>项目#[meta_value] => 66006)
[1] =>阵列([meta_id] => 37887 [POST_ID] => 5551 [meta_key] =>六角尺寸[meta_value] => 3/32)
[2] =>阵列([meta_id] => 37888 [POST_ID] => 5551 [meta_key] =>对边[meta_value] => 0.095)
[3] =>阵列([meta_id] => 37889 [POST_ID] => 5551 [meta_key] =>类型[meta_value] =>六角)
[4] =>阵列([meta_id] => 37890 [POST_ID] => 5551 [meta_key] =>柄[meta_value] => .315))

我需要通过数组[meta_value]顺序一一列举。然后,我使用数组生成的顺序产品表。我一直用下面的函数,但它产生的结果没有任何意义:

 函数subval_sort($ A,$子项){
    的foreach($ A $为K => $ V){
        $ B [$ K] =用strtolower($ V [$子键]);
    }
    ASORT($ B);
    的foreach($ B $作为重点=> $ VAL){
        $ C [] = $ A [$关键];
    }
    返回$ C;
}


解决方案

使用 usort(),例如:

  $项目= [
    ['ID'=> 3,'项'= GT; 'PC'],
    ['ID'=> 1,'项'=> '老鼠'],
    ['ID'=> 2,'项'=> 'KB'],
];功能compare_id($ A,$ B){
    如果($ A ['身份证'] == $ B ['身份证'])返回0;
    返回($ A ['身份证']< $ B ['身份证'])? -1:1;
}usort($物品,'compare_id');后续代码var_dump($项目);

或使用匿名函数

  usort($项目,功能($ A,$ B){
    如果($ A ['身份证'] == $ B ['身份证'])返回0;
    返回($ A ['身份证']< $ B ['身份证'])? -1:1;
});

There are similar questions and answers posted for this, but none of them quite match the structure of my array, so apologies if I missed something. This is an array generated by the WordPress wpdb class:

Array ( 
[0] => Array ( [meta_id] => 37850 [post_id] => 5548 [meta_key] => Item # [meta_value] => 66002 ) 
[1] => Array ( [meta_id] => 37851 [post_id] => 5548 [meta_key] => Hex Size [meta_value] => .051" ) 
[2] => Array ( [meta_id] => 37852 [post_id] => 5548 [meta_key] => Across Flats [meta_value] => 0.051 ) 
[3] => Array ( [meta_id] => 37853 [post_id] => 5548 [meta_key] => Type [meta_value] => Hexagonal ) 
[4] => Array ( [meta_id] => 37854 [post_id] => 5548 [meta_key] => Shank [meta_value] => .315" ) ) 

Array ( 
[0] => Array ( [meta_id] => 37910 [post_id] => 5553 [meta_key] => Item # [meta_value] => 66008 ) 
[1] => Array ( [meta_id] => 37911 [post_id] => 5553 [meta_key] => Hex Size [meta_value] => 1/8" ) 
[2] => Array ( [meta_id] => 37912 [post_id] => 5553 [meta_key] => Across Flats [meta_value] => 0.127 ) 
[3] => Array ( [meta_id] => 37913 [post_id] => 5553 [meta_key] => Type [meta_value] => Hexagonal ) 
[4] => Array ( [meta_id] => 37914 [post_id] => 5553 [meta_key] => Shank [meta_value] => .315" ) ) 

Array ( 
[0] => Array ( [meta_id] => 37862 [post_id] => 5549 [meta_key] => Item # [meta_value] => 66004 ) 
[1] => Array ( [meta_id] => 37863 [post_id] => 5549 [meta_key] => Hex Size [meta_value] => 1/16" ) 
[2] => Array ( [meta_id] => 37864 [post_id] => 5549 [meta_key] => Across Flats [meta_value] => 0.063 ) 
[3] => Array ( [meta_id] => 37865 [post_id] => 5549 [meta_key] => Type [meta_value] => Hexagonal ) 
[4] => Array ( [meta_id] => 37866 [post_id] => 5549 [meta_key] => Shank [meta_value] => .315" ) ) 

Array ( 
[0] => Array ( [meta_id] => 37886 [post_id] => 5551 [meta_key] => Item # [meta_value] => 66006 ) 
[1] => Array ( [meta_id] => 37887 [post_id] => 5551 [meta_key] => Hex Size [meta_value] => 3/32" ) 
[2] => Array ( [meta_id] => 37888 [post_id] => 5551 [meta_key] => Across Flats [meta_value] => 0.095 ) 
[3] => Array ( [meta_id] => 37889 [post_id] => 5551 [meta_key] => Type [meta_value] => Hexagonal ) 
[4] => Array ( [meta_id] => 37890 [post_id] => 5551 [meta_key] => Shank [meta_value] => .315" ) ) 

I need to list them by order of Array[meta_value]. Then I use the array to generate a table of products in that order. I have been working with the following function, but it produces a result that doesn't make any sense:

function subval_sort($a,$subkey) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    asort($b);
    foreach($b as $key=>$val) {
        $c[] = $a[$key];
    }
    return $c;
}

解决方案

Use usort(), example:

$items = [
    ['id' => 3, 'item' => 'pc'],
    ['id' => 1, 'item' => 'mouse'],
    ['id' => 2, 'item' => 'kb'],
];

function compare_id($a, $b) {
    if ($a['id'] == $b['id']) return 0;
    return ($a['id'] < $b['id']) ? -1 : 1;
}

usort($items, 'compare_id');

var_dump($items);    

or using anonymous function

usort($items, function ($a, $b) {
    if ($a['id'] == $b['id']) return 0;
    return ($a['id'] < $b['id']) ? -1 : 1;
});

这篇关于排序多维数组由子阵列键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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