Sort [key => value] array by another array of keys with same index [duplicate]












0
















This question already has an answer here:




  • How can I sort arrays and data in PHP?

    9 answers




I have an array of indexes that I want to sort my other array:



$order = [7, 2, 1, 4];

$array = [
1 => "O",
2 => "T"
4 => "F"
7 => "S"
]


How can I order the $array based on $order array, so that the output is..



$array = [
7 => "S",
2 => "T"
1 => "O",
4 => "F"
]


As far as I read, something other than for loop is much preferred










share|improve this question













marked as duplicate by deceze arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 19:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0
















    This question already has an answer here:




    • How can I sort arrays and data in PHP?

      9 answers




    I have an array of indexes that I want to sort my other array:



    $order = [7, 2, 1, 4];

    $array = [
    1 => "O",
    2 => "T"
    4 => "F"
    7 => "S"
    ]


    How can I order the $array based on $order array, so that the output is..



    $array = [
    7 => "S",
    2 => "T"
    1 => "O",
    4 => "F"
    ]


    As far as I read, something other than for loop is much preferred










    share|improve this question













    marked as duplicate by deceze arrays
    Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Dec 28 '18 at 19:36


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0









      This question already has an answer here:




      • How can I sort arrays and data in PHP?

        9 answers




      I have an array of indexes that I want to sort my other array:



      $order = [7, 2, 1, 4];

      $array = [
      1 => "O",
      2 => "T"
      4 => "F"
      7 => "S"
      ]


      How can I order the $array based on $order array, so that the output is..



      $array = [
      7 => "S",
      2 => "T"
      1 => "O",
      4 => "F"
      ]


      As far as I read, something other than for loop is much preferred










      share|improve this question















      This question already has an answer here:




      • How can I sort arrays and data in PHP?

        9 answers




      I have an array of indexes that I want to sort my other array:



      $order = [7, 2, 1, 4];

      $array = [
      1 => "O",
      2 => "T"
      4 => "F"
      7 => "S"
      ]


      How can I order the $array based on $order array, so that the output is..



      $array = [
      7 => "S",
      2 => "T"
      1 => "O",
      4 => "F"
      ]


      As far as I read, something other than for loop is much preferred





      This question already has an answer here:




      • How can I sort arrays and data in PHP?

        9 answers








      php arrays sorting






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 19:29









      sentysenty

      3,250551129




      3,250551129




      marked as duplicate by deceze arrays
      Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 28 '18 at 19:36


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by deceze arrays
      Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 28 '18 at 19:36


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          2














          You could make use of uksort and array_search:



          uksort($array, function ($key1, $key2) use ($order) {
          return array_search($key1, $order) - array_search($key2, $order);
          });


          Demo here: https://3v4l.org/TlWmP






          share|improve this answer
























          • Clear and simple! Thanks

            – senty
            Dec 28 '18 at 19:42


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You could make use of uksort and array_search:



          uksort($array, function ($key1, $key2) use ($order) {
          return array_search($key1, $order) - array_search($key2, $order);
          });


          Demo here: https://3v4l.org/TlWmP






          share|improve this answer
























          • Clear and simple! Thanks

            – senty
            Dec 28 '18 at 19:42
















          2














          You could make use of uksort and array_search:



          uksort($array, function ($key1, $key2) use ($order) {
          return array_search($key1, $order) - array_search($key2, $order);
          });


          Demo here: https://3v4l.org/TlWmP






          share|improve this answer
























          • Clear and simple! Thanks

            – senty
            Dec 28 '18 at 19:42














          2












          2








          2







          You could make use of uksort and array_search:



          uksort($array, function ($key1, $key2) use ($order) {
          return array_search($key1, $order) - array_search($key2, $order);
          });


          Demo here: https://3v4l.org/TlWmP






          share|improve this answer













          You could make use of uksort and array_search:



          uksort($array, function ($key1, $key2) use ($order) {
          return array_search($key1, $order) - array_search($key2, $order);
          });


          Demo here: https://3v4l.org/TlWmP







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 19:38









          JetoJeto

          5,05521018




          5,05521018













          • Clear and simple! Thanks

            – senty
            Dec 28 '18 at 19:42



















          • Clear and simple! Thanks

            – senty
            Dec 28 '18 at 19:42

















          Clear and simple! Thanks

          – senty
          Dec 28 '18 at 19:42





          Clear and simple! Thanks

          – senty
          Dec 28 '18 at 19:42



          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'