dongzai2952
2018-01-12 11:16
浏览 66
已采纳

在php和wordpress中合并然后排序2个数组

I'm trying to merge 2 arrays in PHP and then sort them.

I'm pulling data from WordPress database ( tags and categories ) and I need to merge them and sort them properly.

Code looks similar to this:

$categories = [ 
  '<a>WordPress Tips</a>',
  '<a>SEO Tips'</a>, 
  '<a>Development Tips</a>' 
]; 

$tags = [ 
  '<a>WordPress</a>',
  '<a>SEO'</a>, 
  '<a>Development</a>' 
]; 

$taxonomies = array_merge($categories, $tags);
sort($taxonomies);

Since I need to loop through $taxonomies array and print them with anchors included ( every one of those must have a valid URL to itself ), I don't get a proper sorting result. However, as soon as I 'strip out' all the tags around these items, sorting works as it should, but I don't get URLs that I need, only string / text.

Could someone suggest a better sorting algorithm which would sort these items properly with html elements included around them? Thanks!

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在PHP中合并2个数组,然后对它们进行排序。 < 我正在从WordPress数据库(标签和类别)中提取数据,我需要将它们合并并正确排序。

代码看起来类似于: \ n

  $ categories = [
'&lt; a&gt; WordPress提示&lt; / a&gt;',
'&lt; a&gt; SEO提示'&lt; / a&gt;,
'&lt; a&gt;开发 提示和LT; / A&GT;”  \ N];  
 
 $ tags = [
'&lt; a&gt; WordPress&lt; / a&gt;',
'&lt; a&gt; SEO'&lt; / a&gt;,
'&lt; a&gt;开发&lt; / a&gt;'  \ N];  
 
 $ taxonomies = array_merge($ categories,$ tags); 
sort($ taxonomies); 
   
 
 

因为我需要遍历$ taxonomies数组并且 打印它们包含锚点(每个必须有一个有效的URL自己),我没有得到一个正确的排序结果。 但是,只要我“删除”这些项目周围的所有标记,排序就可以正常工作,但我没有得到我需要的URL,只有字符串/文本。

有人可以建议一个更好的排序算法,它会使用包含在它们周围的html元素正确排序这些项目吗? 谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题