Php排序数组项问题
大家好,我有一系列的季节和集名称数组 这是我的数组Php排序数组项问题,php,arrays,Php,Arrays,大家好,我有一系列的季节和集名称数组 这是我的数组 - 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 Flas
- 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
我尝试了一个ORT函数,但没有成功。大多数项目的顺序正确,但有些项目不正确。以下是我的完整代码:
$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';}
我该怎么做?
非常感谢。您正在寻找的是自然排序,可以使用以下功能实现:
<?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>';
对于对象数组,必须使用和:
你的代码是什么?你试过什么?预期产量是多少,实际产量是多少?我们可以帮助您解决代码中的问题,但我们无法理解您的想法。哦,也许可以用代码中定义的格式提供输入数据。我在帖子中提到过。我试了一个故事($集);只需发布相关代码。而且不仅仅是一个函数的名称,没有足够的关于它是如何使用的信息。为什么它对你不起作用,你的代码是什么样子的?从您当前的问题来看还不清楚。我有$SECTIONS数组用于项目,每个项目都有以下格式的全文:Flash 1.Sezon 5.Bölüm for循环我正在打印它们以进行调试,但顺序不正确。它给出了500个内部服务器错误。我唯一想做的就是用natsort替换asort。我再次替换为asort并修复了500个问题。为什么它对我不起作用?可能是php版本?您有什么版本的php?根据文档,它应该适用于PHP4、PHP5、PHP7。检查apache错误日志以查看错误[05-Feb-2016 09:03:22 UTC]PHP可捕获致命错误:类WP_Post的对象无法转换为/home/dizipico/public_html/WP content/themes/tema/functions.PHP中的字符串,第290行[05-Feb-2016 09:03:25 UTC]PHP可捕获致命错误:WP_Post类的对象无法转换为第290行/home/dizipico/public_html/WP content/themes/tema/functions.PHP中的字符串。我现在看到您的数组包含对象。。让我更新答案
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"
}
usort($episodes, function($a, $b) {
return strnatcmp($a->post_title, $b->post_title);
});