One array is sorted by another array.

Source: Internet
Author: User
{Code...} known $ a. Sort $ B into $ c by referring to $.
$ A = Array ('cid' => 1, 'cname' => 'about'), Array ('cid' => 7, 'cname' => 'Introduction'), Array ('cid' => 8, 'cname' => 'cultural '), Array ('cid' => 9, 'cname' => 'dynamic '); $ B = Array ('cid' => 1, 'cname' =>' '), array ('cid' => 2, 'cname' => 'Opera '), Array ('cid' => 3, 'cname' => 'meiju '), array ('cid' => 4, 'cname' => 'video'), Array ('cid' => 5, 'cname' => 'enter '), array ('cid' => 6, 'cname' => 'contacts '), Array ('cid' => 7, 'cname' => 'Introduction '), array ('cid' => 8, 'cname' => 'cultural '), Array ('cid' => 9, 'cname' => 'dynamic ')); $ c = Array ('cid' => 1, 'cname' => 'about'), Array ('cid' => 7, 'cname' => 'Introduction'), Array ('cid' => 8, 'cname' => 'cultural '), Array ('cid' => 9, 'cname' => 'dynamic '), Array ('cid' => 2, 'cname' => 'Opera'), Array ('cid' => 3, 'cname' => 'meiju '), Array ('cid' => 4, 'cname' => 'video'), Array ('cid' => 5, 'cname' => 'enter'), Array ('cid' => 6, 'cname' => 'Contact '));

$ A is known, and $ B is sorted into $ c by reference to $.

Reply content:
$ A = Array ('cid' => 1, 'cname' => 'about'), Array ('cid' => 7, 'cname' => 'Introduction'), Array ('cid' => 8, 'cname' => 'cultural '), Array ('cid' => 9, 'cname' => 'dynamic '); $ B = Array ('cid' => 1, 'cname' =>' '), array ('cid' => 2, 'cname' => 'Opera '), Array ('cid' => 3, 'cname' => 'meiju '), array ('cid' => 4, 'cname' => 'video'), Array ('cid' => 5, 'cname' => 'enter '), array ('cid' => 6, 'cname' => 'contacts '), Array ('cid' => 7, 'cname' => 'Introduction '), array ('cid' => 8, 'cname' => 'cultural '), Array ('cid' => 9, 'cname' => 'dynamic ')); $ c = Array ('cid' => 1, 'cname' => 'about'), Array ('cid' => 7, 'cname' => 'Introduction'), Array ('cid' => 8, 'cname' => 'cultural '), Array ('cid' => 9, 'cname' => 'dynamic '), Array ('cid' => 2, 'cname' => 'Opera'), Array ('cid' => 3, 'cname' => 'meiju '), Array ('cid' => 4, 'cname' => 'video'), Array ('cid' => 5, 'cname' => 'enter'), Array ('cid' => 6, 'cname' => 'Contact '));

$ A is known, and $ B is sorted into $ c by reference to $.

Use the custom sorting 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 Sorting Problem. array_multisort in php can be used for multi-dimensional array sorting.
The specific implementation code will not be written. You can take a look at the manual.

/*** [The oneArrayTo two-dimensional array sorts one-to-multiple objects by an array value] * @ param [type] $ a [reference array] * @ param [type] $ B [This is sorted array] * @ param [type] $ keyword [key value] * @ return [type] [description] */function oneArrayTo ($, $ 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 ;}

I wrote it myself, though stupid.

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.