An array is sorted according to another array

Source: Internet
Author: User
Keywords Php
Tags php class
$a = Array (' cid ' = 1, ' cname ' = ' about '), Array (' cid ' = 7, '  CNAME ' = ' introduction '), Array (' cid ' = 8, ' cname ' = ' culture '), Array (' cid ' = 9, ' cname ' = ' dynamic '); $b = Array (' cid ' = 1, ' cname ' = ' about '), Arra    Y (' cid ' = 2, ' cname ' = ' opera '), Array (' cid ' = 3, ' cname ' = ' US drama ') ), Array (' cid ' = 4, ' cname ' = ' video '), Array (' cid ' = 5, ' CNAME ')        = ' entertainment '), Array (' cid ' = 6, ' cname ' = ' contact '), Array (' cid ' = 7,   ' CNAME ' = ' profile '), Array (' cid ' = 8, ' cname ' = ' culture '), Array (' CID '    = 9, ' cname ' = ' dynamic '), $c = Array (' cid ' = 1, ' cname ' = ' about '),   Array (' CID '= 7, ' cname ' = ' profile '), Array (' cid ' = 8, ' cname ' = ' culture '), Array (    ' CID ' = 9, ' cname ' = ' dynamic '), Array (' cid ' = + 2, ' cname ' = ' opera '), Array (' cid ' = 3, ' cname ' = ' US drama '), Array (' cid ' = + 4, ' cname ' = = ') Video '), Array (' cid ' = 5, ' cname ' = ' entertainment '), Array (' cid ' = + 6, ' CN Ame ' = ' contact ');

Known as $ A, refer to $ A to sort $b into $c.

Reply content:

$a = Array (' cid ' = 1, ' cname ' = ' about '), Array (' cid ' = 7, '  CNAME ' = ' introduction '), Array (' cid ' = 8, ' cname ' = ' culture '), Array (' cid ' = 9, ' cname ' = ' dynamic '); $b = Array (' cid ' = 1, ' cname ' = ' about '), Arra    Y (' cid ' = 2, ' cname ' = ' opera '), Array (' cid ' = 3, ' cname ' = ' US drama ') ), Array (' cid ' = 4, ' cname ' = ' video '), Array (' cid ' = 5, ' CNAME ')        = ' entertainment '), Array (' cid ' = 6, ' cname ' = ' contact '), Array (' cid ' = 7,   ' CNAME ' = ' profile '), Array (' cid ' = 8, ' cname ' = ' culture '), Array (' CID '    = 9, ' cname ' = ' dynamic '), $c = Array (' cid ' = 1, ' cname ' = ' about '),   Array (' CID '= 7, ' cname ' = ' profile '), Array (' cid ' = 8, ' cname ' = ' culture '), Array (    ' CID ' = 9, ' cname ' = ' dynamic '), Array (' cid ' = + 2, ' cname ' = ' opera '), Array (' cid ' = 3, ' cname ' = ' US drama '), Array (' cid ' = + 4, ' cname ' = = ') Video '), Array (' cid ' = 5, ' cname ' = ' entertainment '), Array (' cid ' = + 6, ' CN Ame ' = ' contact ');

Known as $ A, refer to $ A to sort $b into $c.

Use the custom sort function 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);?>

This should be a multi-dimensional array ordering problem, PHP Array_multisort can be used for multidimensional array sorting.
The specific implementation of the code will not write, you can look at the manual to ponder.

/** * [Onearrayto two-dimensional array is sorted by an array value one-to-many] * @param [type] $a [reference array] * @param [Type] $b [This is the array to be sorted] * @param [Type] $keyword [key value] * @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;} 

Write your own, although it is a stupid method.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.