2017-04-06 137 views
0

我需要按优先顺序排列具有键值对的json数组(特殊字符>数字>小写>大写)。我试着用ascii代码,但无法达到预期效果。php usort()与json排序的优先顺序

$arr1 = array (
    0 => 
    array (
    'id' => 1, 
    'name' => 'B', 
    'value' => 'abc', 
    'order' => 6, 
), 
    1 => 
    array (
    'id' => 2, 
    'name' => 'a', 
    'value' => 'xyz', 
    'order' => 2, 
), 
    2 => 
    array (
    'id' => 3, 
    'name' => 'A', 
    'value' => 'ghi', 
    'order' => 1, 
), 
    3 => 
    array (
    'id' => 4, 
    'name' => '123', 
    'value' => 'xyz', 
    'order' => 2, 
), 
    4 => 
    array (
    'id' => 5, 
    'name' => 'd', 
    'value' => 'uvw', 
    'order' => 3, 
), 
    5 => 
    array (
    'id' => 6, 
    'name' => '@2', 
    'value' => 'def', 
    'order' => 3, 
), 
); 
function cmp($a, $b) 
{ 
    $at = iconv('UTF-8', 'ASCII//TRANSLIT', $a['name']); 
    $bt = iconv('UTF-8', 'ASCII//TRANSLIT', $b['name']); 
    return strcmp($at, $bt); 
} 
usort($arr1, "cmp"); 
print_r($arr1); 

任何人都可以帮我解决它吗?

回答

0
<?php 
// Obtain a list of columns 
foreach ($arr1 as $key => $row) { 
    $arr1[$key] = $row['value']; 
} 

ksort($ arr1); ?>

+0

尝试不按照我的优先顺序排序。 – Indhu

+0

@Indhu检查我的答案更新 –

+0

不行不行。 – Indhu