Home >Backend Development >PHP Tutorial >一个数组根据另一个数组来排序

一个数组根据另一个数组来排序

WBOY
WBOYOriginal
2016-06-06 20:06:201912browse

<code>$a = Array
(
    Array (
        'cid'   => 1,
        'cname' => '关于'
    ),

    Array (
        'cid'   => 7,
        'cname' => '简介'
    ),

    Array (
        'cid'   => 8,
        'cname' => '文化'
    ),

    Array (
        'cid'   => 9,
        'cname' => '动态'
    )

);

$b = Array
(
    Array (
        'cid'   => 1,
        'cname' => '关于'
    ),

    Array (
        'cid'   => 2,
        'cname' => '歌剧'
    ),

    Array (
        'cid'   => 3,
        'cname' => '美剧'
    ),

    Array (
        'cid'   => 4,
        'cname' => '视频'
    ),

    Array (
        'cid'   => 5,
        'cname' => '娱乐'
    ),

    Array (
        'cid'   => 6,
        'cname' => '联系'
    ),

    Array (
        'cid'   => 7,
        'cname' => '简介'
    ),

    Array (
        'cid'   => 8,
        'cname' => '文化'
    ),

    Array (
        'cid'   => 9,
        'cname' => '动态'
    )

);
$c = Array
(
    Array (
        'cid'   => 1,
        'cname' => '关于'
    ),

    Array (
        'cid'   => 7,
        'cname' => '简介'
    ),

    Array (
        'cid'   => 8,
        'cname' => '文化'
    ),

    Array (
        'cid'   => 9,
        'cname' => '动态'
    ),

    Array (
        'cid'   => 2,
        'cname' => '歌剧'
    ),

    Array (
        'cid'   => 3,
        'cname' => '美剧'
    ),

    Array (
        'cid'   => 4,
        'cname' => '视频'
    ),

    Array (
        'cid'   => 5,
        'cname' => '娱乐'
    ),

    Array (
        'cid'   => 6,
        'cname' => '联系'
    )
);</code>

已知$a,参照$a把$b排序成$c的样子。

回复内容:

<code>$a = Array
(
    Array (
        'cid'   => 1,
        'cname' => '关于'
    ),

    Array (
        'cid'   => 7,
        'cname' => '简介'
    ),

    Array (
        'cid'   => 8,
        'cname' => '文化'
    ),

    Array (
        'cid'   => 9,
        'cname' => '动态'
    )

);

$b = Array
(
    Array (
        'cid'   => 1,
        'cname' => '关于'
    ),

    Array (
        'cid'   => 2,
        'cname' => '歌剧'
    ),

    Array (
        'cid'   => 3,
        'cname' => '美剧'
    ),

    Array (
        'cid'   => 4,
        'cname' => '视频'
    ),

    Array (
        'cid'   => 5,
        'cname' => '娱乐'
    ),

    Array (
        'cid'   => 6,
        'cname' => '联系'
    ),

    Array (
        'cid'   => 7,
        'cname' => '简介'
    ),

    Array (
        'cid'   => 8,
        'cname' => '文化'
    ),

    Array (
        'cid'   => 9,
        'cname' => '动态'
    )

);
$c = Array
(
    Array (
        'cid'   => 1,
        'cname' => '关于'
    ),

    Array (
        'cid'   => 7,
        'cname' => '简介'
    ),

    Array (
        'cid'   => 8,
        'cname' => '文化'
    ),

    Array (
        'cid'   => 9,
        'cname' => '动态'
    ),

    Array (
        'cid'   => 2,
        'cname' => '歌剧'
    ),

    Array (
        'cid'   => 3,
        'cname' => '美剧'
    ),

    Array (
        'cid'   => 4,
        'cname' => '视频'
    ),

    Array (
        'cid'   => 5,
        'cname' => '娱乐'
    ),

    Array (
        'cid'   => 6,
        'cname' => '联系'
    )
);</code>

已知$a,参照$a把$b排序成$c的样子。

使用自定义排序函数usort来一发

<code><?php class SortByArray {
  
  public $ref;
  public $array;

  public function __construct($ref,$array){
   $this->ref = $ref;
   $this->array = $array;    
      usort($this->array,function($t1,$t2){
        $p1 = array_search($t1,$this->ref);
        $p2 = array_search($t2,$this->ref);
        if(($p1===false||$p1===null) && ($p2===false||$p2===null))
          return array_search($t1,$this->array)<array_search>array) ? -1 :1;
        if($p1===$p2) return 0;  
        if($p1===false||$p1===null) return 1;
        if($p2===false||$p2===null) return -1; 
        return $p1array);

?></array_search></code>

这个应该是多维数组排序问题,php中的 array_multisort可以用于多维数组排序。
具体实现代码就不写了,可以看看手册自己琢磨一下。

<code>/**
 * [oneArrayTo 二维数组按某个数组值排序 一对多]
 * @param  [type] $a       [参照数组]
 * @param  [type] $b       [这个是要被排序的数组]
 * @param  [type] $keyword [键值]
 * @return [type]          [description]
 */
function oneArrayTo($a,$b,$keyword)
{
    $newa = array();
    foreach ($a as $key => $value) {
        $newa[] = $value[$keyword];
    }
    $newa = array_flip($newa);

    $newb = array();
    foreach ($b as $key => $value) {
        $newb[] = $value[$keyword];
    }
    $newb = array_flip($newb);

    $d = $newb;
    foreach ($newb as $k => $v) {
        foreach ($newa as $key => $value) {
            if($key == $k) {
                unset($d[$k]);
            }
        }
    }
    $c = array_flip($newa);
    $d = array_flip($d);

    $p = array_merge($c,$d);
    $p = array_flip($p);

    $newbb = array();
    foreach ($b as $key => $value) {
        $newbb[$value[$keyword]] = $value;
    }

    $g = array();
    foreach ($p as $key => $value) {
        $g[] = $newbb[$key];
    }
    
    return $g;
}</code>

自己写好了,虽然是笨方法。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn