当前位置:Gxlcms > PHP教程 > 一个数组根据另一个数组来排序

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

时间:2021-07-01 10:21:17 帮助过:65人阅读

$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' => '联系'
    )
);

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

回复内容:

$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' => '联系'
    )
);

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

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

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) ? -1 :1;
        if($p1===$p2) return 0;  
        if($p1===false||$p1===null) return 1;
        if($p2===false||$p2===null) return -1; 
        return $p1<$p2 ? -1 : 1;
      });  
  }
 
}

//----------------------
$res = new SortByArray($a,$b);
print_r($res->array);

?>

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

/**
 * [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;
}

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

人气教程排行