用捷克语用php排序数据数组结构 [英] Sort data array structure in php for Czech language

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

问题描述

我从远程API的php中获取JSON,然后用usort将数据数组从a排序到z.但是我需要按捷克字母排序.

I am getting JSON in php from remote API and after that with usort i am sorting data arrays from a to z. But i need to sort it in Czech alphabet.

这是我当前的代码:

     $body = wp_remote_retrieve_body( $request );
     $data = json_decode( $body, true )['data']['items'];
     usort($data,function($a,$b) {return strnatcasecmp($a['city'],$b['city']);});

你能帮我吗?

谢谢

推荐答案

由于您没有提供数组结构,因此我只是假设了一个.

Since you did not provide the array structure I just assumed one.

$data = array(
    'items' => array(
        0 => array('city' => 'Froni'),
        1 => array('city' => 'Frans'),
        2 => array('city' => 'Frédéric')
    ),
);
 usort($data['items'],
     function($a,$b) {
         $coll = collator_create( 'fr_FR' );
         $aSortKey = collator_get_sort_key($coll, $a['city']);
         $bSortKey = collator_get_sort_key($coll, $b['city']);
         return $aSortKey >= $bSortKey;
     }
 );
var_dump($data['items']);

这是利用整理程序排序键的优势,而不是比较字符串本身,并用于在usort匿名函数中对字符串进行排序.不过,您极有可能必须更改usort函数及其语言环境中使用的参数.

This takes advantage of the Collator Sort Key rather than comparing the strings itself and is used to sort the strings within your usort anonymous function. You will most likely have to change the parameters going in the usort function and its locale, though.

这篇关于用捷克语用php排序数据数组结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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