Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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 usesort在explode函数抓取的区分大小写的字符串中不起作用_Php_Wordpress_Sorting - Fatal编程技术网

Php usesort在explode函数抓取的区分大小写的字符串中不起作用

Php usesort在explode函数抓取的区分大小写的字符串中不起作用,php,wordpress,sorting,Php,Wordpress,Sorting,这段代码非常有效,直到有东西区分大小写。我尝试使用strnatcasecmp而不是cmp,它获取整个字符串,而不是姓氏。有没有关于如何写得更好的建议 例如,当前订单为: 亚伦·卡森 亚当·戴维森 詹·亨宁斯 埃克托蓝口病 我希望ektor bluemouth站在首位 foreach($connected->posts as $p) { $lastname = explode(' ', $p->post_title); if(sizeof($lastname) == 1) {$p-&g

这段代码非常有效,直到有东西区分大小写。我尝试使用strnatcasecmp而不是cmp,它获取整个字符串,而不是姓氏。有没有关于如何写得更好的建议

例如,当前订单为:
亚伦·卡森
亚当·戴维森
詹·亨宁斯
埃克托蓝口病


我希望ektor bluemouth站在首位

foreach($connected->posts as $p) {
$lastname = explode(' ', $p->post_title);
if(sizeof($lastname) == 1) {$p->lastname = $lastname[0];}
if(sizeof($lastname) == 2) {$p->lastname = $lastname[1];}
if(sizeof($lastname) == 3) {$p->lastname = $lastname[2];}
}                           
usort($connected->posts, 'cmp');

我会这样做

$aPosts = array(
    array('post_title' => 'Aaron Carson'),
    array('post_title' => 'Adam Davidson'),
    array('post_title' => 'Jen Hennings'),
    array('post_title' => 'ektor bluemouth'),
);

$aOrderedPosts = array();


foreach($aPosts as $iPos => $p){
    $a = explode(' ', $p['post_title']);
    // cast the last name to lowercase and assign by reference the post
    $aOrderedPosts[strtolower($a[1])] =& $aPosts[$iPos];
    }

var_dump($aOrderedPosts);
// perform a key sort
ksort($aOrderedPosts);
var_dump($aOrderedPosts);
它可以调整为与您的数据格式一起使用

$aOrderedPosts = array();

foreach($connected->posts as $iPos => $p){
    $a = explode(' ', $p->post_title);
    $aOrderedPosts[strtolower($a[1])] =& $connected->posts[$iPos];
}

ksort($aOrderedPosts);
var_dump($aOrderedPosts);

从DB中创建/读入post对象时,最好创建
$aOrderedPosts
。另外,我会将它创建为
连接
类的属性。

我会这样做

$aPosts = array(
    array('post_title' => 'Aaron Carson'),
    array('post_title' => 'Adam Davidson'),
    array('post_title' => 'Jen Hennings'),
    array('post_title' => 'ektor bluemouth'),
);

$aOrderedPosts = array();


foreach($aPosts as $iPos => $p){
    $a = explode(' ', $p['post_title']);
    // cast the last name to lowercase and assign by reference the post
    $aOrderedPosts[strtolower($a[1])] =& $aPosts[$iPos];
    }

var_dump($aOrderedPosts);
// perform a key sort
ksort($aOrderedPosts);
var_dump($aOrderedPosts);
它可以调整为与您的数据格式一起使用

$aOrderedPosts = array();

foreach($connected->posts as $iPos => $p){
    $a = explode(' ', $p->post_title);
    $aOrderedPosts[strtolower($a[1])] =& $connected->posts[$iPos];
}

ksort($aOrderedPosts);
var_dump($aOrderedPosts);
从DB中创建/读入post对象时,最好创建
$aOrderedPosts
。此外,我将创建它作为
连接
类的属性