2016-02-05 40 views
-4

大家好我有阵列系列季和剧集的名字 这里是我的阵列PHP的排序数组项问题

- The Flash 2.Sezon 11.Bölüm 
- The Flash 2.Sezon 1.Bölüm 
- The Flash 1.Sezon 20.Bölüm 
- The Flash 2.Sezon 3.Bölüm 
- The Flash 3.Sezon 3.Bölüm 

(sezon =>季节,bolum =>情节在我的语言)

我要首发排序他们最后一集

- The Flash 1.Sezon 20.Bölüm 
- The Flash 2.Sezon 1.Bölüm 
- The Flash 2.Sezon 3.Bölüm 
- The Flash 2.Sezon 11.Bölüm 
- The Flash 3.Sezon 3.Bölüm 

我试图ASORT功能,但没有奏效。以正确的顺序大多数项目,但他们中的一些not.Here是我的全码:

$args=array('post_type' => 'bolum', 
      'posts_per_page' =>-1, 
      'orderby' => 'title', 
      'meta_query' => array(array(
            'key' => 'bolum_dizi', 
            'value' => '"' . $post_id . '"', // matches exaclty "123", not just 123. This prevents a match for "1234" 
            'compare' => 'LIKE' 
             ) 
           ) 
      ); 
$episodes = get_posts($args); 

这也是我如何订购和打印

asort($episodes); 
foreach($episodes as $episode){ 
echo $episode->post_title .'\n';} 

我怎样才能做到这一点? 非常感谢。

+3

你的代码是什么?你有什么尝试?预期产出是多少?实际产出是多少?我们可以帮助您解决代码中的问题,但我们无法理解您的想法。哦,也许可以用你的代码中定义的相同格式给你的输入数据。 –

+0

我在帖子中提到过。我尝试了($ episodes); –

+2

只是发布相关的代码。而不仅仅是一个功能的名称,没有足够的信息来说明它是如何使用的。 –

回答

2

你要找的是所谓自然排序可以使用natsort()功能来实现:

<?php 

$arr = array(
'- The Flash 2.Sezon 11.Bölüm', 
'- The Flash 2.Sezon 1.Bölüm', 
'- The Flash 1.Sezon 20.Bölüm', 
'- The Flash 2.Sezon 3.Bölüm', 
'- The Flash 3.Sezon 3.Bölüm', 
); 

natsort($arr); 

echo '<pre>'; 
var_dump($arr); 
echo '</pre>'; 

其结果将是:

array(5) { 
    [2]=> 
    string(30) "- The Flash 1.Sezon 20.Bölüm" 
    [1]=> 
    string(29) "- The Flash 2.Sezon 1.Bölüm" 
    [3]=> 
    string(29) "- The Flash 2.Sezon 3.Bölüm" 
    [0]=> 
    string(30) "- The Flash 2.Sezon 11.Bölüm" 
    [4]=> 
    string(29) "- The Flash 3.Sezon 3.Bölüm" 
} 

对于对象,你必须数组使用usortstrnatcmp

usort($episodes, function($a, $b) { 
    return strnatcmp($a->post_title, $b->post_title); 
}); 
+0

它给了500内部服务器错误。只是认为我做的是用natsort替换asort。我再次更换asort并修复了500个问题。为什么它不适合我?也许PHP版本? –

+0

你有什么版本的PHP? –

+0

根据文档,它应该可用于PHP 4,PHP 5,PHP 7.检查Apache的error_log看看有什么不对 –