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!

  • 写回答

2条回答 默认 最新

  • duanci19881003 2018-01-12 11:41
    关注

    You need to do custom comaprision to make it work using usort() and strip_tags()

    <?php
    $taxonomies = array_merge($categories, $tags);
    print_r($taxonomies);
    function cmp($a, $b){
        $data1 = strip_tags($a);
        $data2 = strip_tags($b);
        return ($data1 < $data2) ? -1 : 1;
    }
    usort($taxonomies, "cmp");
    print_r($taxonomies);
    

    Output:-https://eval.in/934071

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,