Php 如何对两个包含4位数字(年份)的字符串进行排序?
我想每年对两个州进行分类,以便先是1920年的州,然后是1930年的州 首先,我将两个字符串按(.)分解,然后使用natsort按年份对它们进行排序……但它不起作用。到目前为止,我所做的是:Php 如何对两个包含4位数字(年份)的字符串进行排序?,php,sorting,Php,Sorting,我想每年对两个州进行分类,以便先是1920年的州,然后是1930年的州 首先,我将两个字符串按(.)分解,然后使用natsort按年份对它们进行排序……但它不起作用。到目前为止,我所做的是: <?php $strn="The Muslim League slowly rose to mass popularity in the **1930s** amid fears of under-representation and neglect of Muslims in politics.T
<?php
$strn="The Muslim League slowly rose to mass popularity in the **1930s** amid fears of under-representation and neglect of Muslims in politics.The largely non-violent independence struggle led by the Indian Congress engaged millions of protesters in mass campaigns of civil disobedience in the **1920s**";
$result = explode('.',$strn);
natsort($result);
echo $result[0];
echo $result[1];
?>
这应该适合您: (在这里,我只是从每个字符串中提取年份,并对字符串进行比较并按年份进行排序) 输出:
Array
(
[0] => The largely non-violent independence struggle led by the Indian Congress engaged millions of protesters in mass campaigns of civil disobedience in the **1920s**
[1] => The Muslim League slowly rose to mass popularity in the **1930s** amid fears of under-representation and neglect of Muslims in politics
)
此外,如果要再次将其打印为字符串,请使用:
echo implode(".", $strn);
什么是a美元,b美元?不是应该是$strn吗?ps:我没有得到你得到的输出。@candycrush
$a
和$b
是$strn
的元素,可以进行比较和排序(附言:@candycrush^请查看最后一条评论中的链接,这里有一个链接。否则,请发布一个粘贴箱,或者介绍您如何使用代码。非常好……完美……非常感谢您的时间和努力。您太好了。:)
echo implode(".", $strn);