按子数组键值对多维数组进行排序

时间:2021-12-24 21:34:20

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:

为此发布了类似的问题和答案,但没有一个与我的阵列的结构完全匹配,所以如果我错过了什么,请道歉。这是由WordPress wpdb类生成的数组:

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:

我需要按照Array [meta_value]的顺序列出它们。然后我使用该数组按顺序生成产品表。我一直在使用以下函数,但它产生的结果没有任何意义:

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;
}

1 个解决方案

#1


22  

Use usort(), example:

使用usort(),例如:

$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;
});

#1


22  

Use usort(), example:

使用usort(),例如:

$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;
});