Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP按字母顺序排序的UTF-8标记(未找到类';Collator') 编辑的问题。2013年12月18日后生效。_Php_Arrays_Sorting_Collation_Alphabetical Sort - Fatal编程技术网

PHP按字母顺序排序的UTF-8标记(未找到类';Collator') 编辑的问题。2013年12月18日后生效。

PHP按字母顺序排序的UTF-8标记(未找到类';Collator') 编辑的问题。2013年12月18日后生效。,php,arrays,sorting,collation,alphabetical-sort,Php,Arrays,Sorting,Collation,Alphabetical Sort,在SO和google上进行了更多的搜索之后,我决定可能需要使用和函数,因为我的站点语言是土耳其语,我使用UTF-8编码 我现在的新职位是: /* fetch values */ $etiket_bulutu = ''; while ($beyan->fetch()) { $etiket_bulutu .= $tags.', '; } $etiket_bulutu = substr_replace($etiket_bulutu ,'',-2); //omit last {, }

在SO和google上进行了更多的搜索之后,我决定可能需要使用和函数,因为我的站点语言是土耳其语,我使用UTF-8编码

我现在的新职位是:

/* fetch values */
$etiket_bulutu = '';
while ($beyan->fetch()) 
{
    $etiket_bulutu .=  $tags.', ';
}

$etiket_bulutu = substr_replace($etiket_bulutu ,'',-2); //omit last {, } chars

$etiketler = explode(", ", $etiket_bulutu); //get each tag as arr

$etiketler = array_unique($etiketler);
$etiketler = array_values($etiketler); //only unique tags without NULLs

$etadet = count($etiketler);

$coll = collator_create('tr_TR'); //from http://www.php.net/manual/en/collator.sort.php
collator_sort($coll, $etiketler);

for($x=0;$x<$etadet;$x++)
 {
  echo $etiketler[$x];
  echo "<br />";
 }
回显“\t”。”。“\r\n”; 回显“\t\t”。“Etiketler”。“\r\n”

$etiket_bulutu=替换($etiket_bulutu,,-2)//我的儿子是亚尼·亚纳特克(Arakteri yani{,}attık) $etiket_bulutu=爆炸(“,”,$etiket_bulutu)//她的bir etiketten阵列yarattık 排序($etiket_bulutu,sort_NATURAL | sort_FLAG_CASE); $etiket\u bulutu=array\u unique($etiket\u bulutu,SORT\u STRING)//萨迪克·泰克尔·埃蒂克特勒·卡尔德 $bulut=''; foreach($etiket\u bulutu作为$etiket) {$bulut.='''.convert_one_row($etiket)。',';} $bulut=替换子项($bulut,,-2)//我的儿子是亚尼·亚纳特克(Arakteri yani{,}attık) echo“\r\n\r\n”。$bulut.

。“\r\n\r\n”

回显“\t”。”。“\r\n”
/*结束语*/ $beyan->close(); } ?>

我目前的成绩 我实现了打印标签与相关的链接,但字母排序问题已失败。目前,第一篇文章的标签是写然后第二篇文章的标签,第三等。。。但是没有按字母顺序排序

当前情况示例:

第1条标签:桌子、苹果、智能手机

第2条标签:智能手机、世界、足球

当前情况:
table、苹果、智能手机、世界、足球

我需要什么:
苹果、智能手机、足球、桌子

你能纠正我吗
提前谢谢你

我对你的代码做了一个快速的本地检查,它对我有效。我就是这么做的:

foreach($etiket_bulutu as $etiket){
    $bulut .= $etiket.', ';
}
$bulut = substr_replace($bulut ,'',-2);
给定的输出是:苹果、智能手机、足球、桌子、世界

所以你的代码基本上是正确的,但我觉得有点复杂。为什么它不起作用我现在真的不知道。数据库数据中是否有任何不可见的标志?将数据库中的所有数据放入$etiket_bulut后,$etiket_bulut看起来是什么样子?这和我的代码一样吗? 尝试在排序之前执行数组_唯一。也许这有帮助

我建议你对不同的事情使用不同的变量。$etiket_bulut首先是一个字符串,然后是一个数组。在我看来,这是一种糟糕的编码风格。如果有人看了代码没有看到explode(),他会想知道为什么要对字符串使用数组函数

你可以简化这一点:

$bulut = implode(', ', $etiket_bulutu);
进入

在问了这个问题并了解了答案之后,我需要找到一种方法,按照字母顺序对MySql db中的UTF-8(土耳其输入)字符串数组进行排序

然后我在中使用了Fy-的答案,结果成功了

现在,我的解决方案是:

Optional sorting type, one of the following:

    Collator::SORT_REGULAR - compare items normally (don't change types)

    Collator::SORT_NUMERIC - compare items numerically

    Collator::SORT_STRING - compare items as strings

Default sorting type is Collator::SORT_REGULAR. It is also used if an invalid sort_flag value has been specified.
setlocale(LC_COLLATE,'tr_tr.utf8')//gerekli的utf-8分拣iç
$sorgum=“…”;
$BindParameter1='…';
如果($beyan=$db_baglanti->准备($sorgum))
{
/*绑定参数*/
$beyan->bind_param(“s”,$bindParameter1);
/*执行语句*/
$beyan->execute();
/*绑定结果变量*/
$beyan->bind_结果($etiketler);
回显“\t”。”。“\r\n”;
回显“\t\t”。“Etiketler”。“\r\n”;
/*获取值*/
$etiket_bulutu='';
而($beyan->fetch())
{
$etiket_bulutu.=$etiketler.,';
}
$etiket_bulutu=substr_replace($etiket_bulutu,,,-2)///en-son 2karakteri yani{,}attık
$etiketler=explode(“,”,$etiket_bulutu);//她的bir-etiketten数组yarattık
$etiketler=array_unique($etiketler);
$etiketler=数组_值($etiketler);//sadeceTekil etiketler kaldı
$etadet=计数($etiketler);
usort($etiketler,'strcoll');
回显“\n\t\t”。。“\n”;

对于($x=0;$x),排序标志Collator::sort()中缺少一个参数。如文档所述:

排序标志

//example of an array with words in turkish
$etiketler = ['İyiyim', 'sabah', 'şemsiye', 'Ağaç', 'çay', 'çanta', 'üzüm', 'şeftali', 'çilek', 'maltaeriği'];
//Make a Collator object with the turkish locale
$collator = new Collator('tr_TR');
//Notice the parameter: Collator::SORT_REGULAR It is the sort flag, it instructs the function as to how the sorting could be done, as there are other sorting possibilities.
$collator->sort($etiketler, Collator::SORT_REGULAR);

var_dump($etiketler);
//ordered array:
/*
array(10) {
  [0]=>
  string(6) "Ağaç"
  [1]=>
  string(6) "çanta"
  [2]=>
  string(4) "çay"
  [3]=>
  string(6) "çilek"
  [4]=>
  string(7) "İyiyim"
  [5]=>
  string(11) "maltaeriği"
  [6]=>
  string(5) "sabah"
  [7]=>
  string(8) "şeftali"
  [8]=>
  string(8) "şemsiye"
  [9]=>
  string(6) "üzüm"
}
*/

所以,在大多数情况下,您应该指定Collator::SORT_REGULAR作为该参数,这样就可以了

在您的情况下,您需要写以下内容:

此外,如果PHP说找不到该类,则可能禁用了国际化函数。要激活这些函数,请使用以下说明: 看看用户的评论,可能很容易从windows中的php.ini中删除1个字符:
或者在linux中运行1命令。

这不仅仅是通过mysql中的SORT BY实现的吗?还有,为什么有多个相同名称的标记(根据唯一要求)
$bulut = implode(', ', $etiket_bulutu);
setlocale(LC_COLLATE, 'tr_TR.utf8'); //utf-8 sorting için gerekli
$sorgum = "...";
$bindparametre1 = '...';

if ($beyan = $db_baglanti->prepare($sorgum)) 
{
    /* bind parameters */
    $beyan -> bind_param("s", $bindparametre1);

    /* execute statement */
    $beyan->execute();

    /* bind result variables */
    $beyan->bind_result($etiketler);

echo "\t".'<div class="sol-icerik-kapsar">'."\r\n";
echo "\t\t".'<h1>'.'Etiketler'.'</h1>'."\r\n";

    /* fetch values */
    $etiket_bulutu = '';
    while ($beyan->fetch()) 
    {
        $etiket_bulutu .=  $etiketler.', ';
    }
$etiket_bulutu = substr_replace($etiket_bulutu ,'',-2); //en son 2karakteri yani {, } attık
$etiketler = explode(", ", $etiket_bulutu); //her bir etiketten array yarattık
$etiketler = array_unique($etiketler);
$etiketler = array_values($etiketler); //sadece tekil etiketler kaldı
$etadet = count($etiketler);

usort($etiketler, 'strcoll');

echo "\n\t\t".'<p>'."\n";
for($x=0;$x<$etadet;$x++)
  {
  echo "\t\t\t".'<a href="'.url_validate(sitenin_koku.'etiketler/'.$etiketler[$x]).'">'.convert_one_row($etiketler[$x]).'</a>, '."\n";
  }
echo "\t\t".'</p>'."\n";
Optional sorting type, one of the following:

    Collator::SORT_REGULAR - compare items normally (don't change types)

    Collator::SORT_NUMERIC - compare items numerically

    Collator::SORT_STRING - compare items as strings

Default sorting type is Collator::SORT_REGULAR. It is also used if an invalid sort_flag value has been specified.
//example of an array with words in turkish
$etiketler = ['İyiyim', 'sabah', 'şemsiye', 'Ağaç', 'çay', 'çanta', 'üzüm', 'şeftali', 'çilek', 'maltaeriği'];
//Make a Collator object with the turkish locale
$collator = new Collator('tr_TR');
//Notice the parameter: Collator::SORT_REGULAR It is the sort flag, it instructs the function as to how the sorting could be done, as there are other sorting possibilities.
$collator->sort($etiketler, Collator::SORT_REGULAR);

var_dump($etiketler);
//ordered array:
/*
array(10) {
  [0]=>
  string(6) "Ağaç"
  [1]=>
  string(6) "çanta"
  [2]=>
  string(4) "çay"
  [3]=>
  string(6) "çilek"
  [4]=>
  string(7) "İyiyim"
  [5]=>
  string(11) "maltaeriği"
  [6]=>
  string(5) "sabah"
  [7]=>
  string(8) "şeftali"
  [8]=>
  string(8) "şemsiye"
  [9]=>
  string(6) "üzüm"
}
*/