PHP按姓氏对数组进行排序,该姓由modx中的空格分隔 [英] PHP sort an array by lastname separated by whitespace in modx

查看:86
本文介绍了PHP按姓氏对数组进行排序,该姓由modx中的空格分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的mysql表:

I have a mysql table that looks like this:

id author public image1 image2 image3 bio media1 media2 media3 media4 media5 media6

字段作者"通常具有以空格分隔的名字(第二名)姓氏. 如何在姓氏"之后对数组进行排序,以及该名字后是否只有一个名字.

The Field "author" normaly has Firstname (Secondname) Lastname seperated by whitespaces. How can I sort the array after the Lastname and if just one name is present after this one.

这是我用来对作者进行排序的modx查询,但显然它不使用姓氏.

This is the modx query I use to sort after the author but obviously it doesn't use the lastname.

  $c = $modx->newQuery('AuthorDe');
  $c->sortby('author','ASC');
  $authors = $modx->getCollection('AuthorDe',$c);

推荐答案

由于以下几个原因,您现在正在朝自己的脚开枪.

You're shooting yourself in the foot right now, for a couple of reasons:

  • 当字符串中只有一个单词时,排序很难预测.
  • 您的数据有索引是有原因的.他们很多快点.使用字符串函数会强制进行表扫描.对于100个数据单元来说足够好了,对于10000行来说速度很慢,并且数据库去度假了"为1000000.
  • 下次您必须使用author字段时,您意识到必须将其拆分为单词,还必须理解并修复旧的代码段.
  • When there is only one word in the string, the sorting is hard to predict.
  • You have indexes for your data for a reason. They make it a lot faster. Using string functions force a table scan. Good enough for 100 data units, slow for 10000 rows and 'database went for a vacation" at 1000000.
  • Next time you have to use the author field and you realize you have to split it up to words you also have to understand and fix this code snippet on top of the old ones.

那是-我还没有测试过-但是可以尝试一下:

That said - I haven't tested it - but try this:

$c->sortby('substring_index(author," ",-1)','ASC');

这篇关于PHP按姓氏对数组进行排序,该姓由modx中的空格分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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