Shuffle array elements by rearranging the key value associations












1














PHP's shuffle() function destroys the array keys, so I decided to write a shuffle function that doesn't do that and instead rearranges key-value associations. As I would be using the function in place of shuffle() most of the time, I'd like it to be as fast and memory efficient (if a time-space tradeoff is needed, I think I would prioritise time) as possible:



<?
function swap(&$a, &$b)
{
$tmp = $a;
$a = $b;
$b = $tmp;
}

function shuffleX(&$arr) #Shuffles the key-value associations in an array.
{
$keys = array_keys($arr); #extract the keys from the array.
$length = count($keys);
$i = 0; #Index.
while ($i < $length-1)
{
$target = rand(($i+1), $length-1); #This ensures that no value ends up mapped to the same key.
swap($arr[$keys[$i]], $arr[$keys[$target]]); #Swap each element of the array with another.
$i++;
}
}
?>









share|improve this question





























    1














    PHP's shuffle() function destroys the array keys, so I decided to write a shuffle function that doesn't do that and instead rearranges key-value associations. As I would be using the function in place of shuffle() most of the time, I'd like it to be as fast and memory efficient (if a time-space tradeoff is needed, I think I would prioritise time) as possible:



    <?
    function swap(&$a, &$b)
    {
    $tmp = $a;
    $a = $b;
    $b = $tmp;
    }

    function shuffleX(&$arr) #Shuffles the key-value associations in an array.
    {
    $keys = array_keys($arr); #extract the keys from the array.
    $length = count($keys);
    $i = 0; #Index.
    while ($i < $length-1)
    {
    $target = rand(($i+1), $length-1); #This ensures that no value ends up mapped to the same key.
    swap($arr[$keys[$i]], $arr[$keys[$target]]); #Swap each element of the array with another.
    $i++;
    }
    }
    ?>









    share|improve this question



























      1












      1








      1







      PHP's shuffle() function destroys the array keys, so I decided to write a shuffle function that doesn't do that and instead rearranges key-value associations. As I would be using the function in place of shuffle() most of the time, I'd like it to be as fast and memory efficient (if a time-space tradeoff is needed, I think I would prioritise time) as possible:



      <?
      function swap(&$a, &$b)
      {
      $tmp = $a;
      $a = $b;
      $b = $tmp;
      }

      function shuffleX(&$arr) #Shuffles the key-value associations in an array.
      {
      $keys = array_keys($arr); #extract the keys from the array.
      $length = count($keys);
      $i = 0; #Index.
      while ($i < $length-1)
      {
      $target = rand(($i+1), $length-1); #This ensures that no value ends up mapped to the same key.
      swap($arr[$keys[$i]], $arr[$keys[$target]]); #Swap each element of the array with another.
      $i++;
      }
      }
      ?>









      share|improve this question















      PHP's shuffle() function destroys the array keys, so I decided to write a shuffle function that doesn't do that and instead rearranges key-value associations. As I would be using the function in place of shuffle() most of the time, I'd like it to be as fast and memory efficient (if a time-space tradeoff is needed, I think I would prioritise time) as possible:



      <?
      function swap(&$a, &$b)
      {
      $tmp = $a;
      $a = $b;
      $b = $tmp;
      }

      function shuffleX(&$arr) #Shuffles the key-value associations in an array.
      {
      $keys = array_keys($arr); #extract the keys from the array.
      $length = count($keys);
      $i = 0; #Index.
      while ($i < $length-1)
      {
      $target = rand(($i+1), $length-1); #This ensures that no value ends up mapped to the same key.
      swap($arr[$keys[$i]], $arr[$keys[$target]]); #Swap each element of the array with another.
      $i++;
      }
      }
      ?>






      performance beginner php array shuffle






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 22 mins ago









      Jamal

      30.3k11116226




      30.3k11116226










      asked yesterday









      Tobi Alafin

      1977




      1977






















          2 Answers
          2






          active

          oldest

          votes


















          3














          Rather than making iterated rand() calls, you should randomize the data once for best efficiency. To do this, just isolate the keys, shuffle them, then rejoin the values to the appropriate keys in the new order.



          Code: (Demo)



          function preserve_shuffle(&$arr)    #Shuffles the key-value associations in an array.
          {
          $keys = array_keys($arr); #extract the keys from the array.
          shuffle($keys);
          for ($index = 0, $length = count($keys); $index < $length; ++$index) {
          $result[$keys[$index]] = $arr[$keys[$index]];
          }
          $arr = $result;
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          preserve_shuffle($arr);
          var_export($arr);


          Or, if you prefer greater brevity or a functional syntax, you could use the following inside your custom function:



          $keys = array_keys($arr);   #extract the keys from the array.
          shuffle($keys);
          $arr = array_merge(array_flip($keys), $arr);


          Proof that it also works: https://3v4l.org/eLMo6





          My earlier snippets only reorder the associative data. The following will shuffle the associations without shuffling the key orders. It does not guarantee that all initially associations will be destroyed -- which I feel is beneficial / less predictable in a randomized result.



          function random_disassociate(&$assoc_array)
          {
          if (sizeof($assoc_array) < 2) {
          return; // data cannot be disassociated
          }
          $keys = array_keys($assoc_array);
          shuffle($assoc_array);
          $assoc_array = array_combine($keys, $assoc_array);
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          random_disassociate($arr);
          var_export($arr);





          share|improve this answer























          • Your function version doesn't work.
            – Tobi Alafin
            13 hours ago










          • That doesn't rearrange the key to value mappings. It just changes the order in which they are presented, but the key to value mappings are the same. The function is supposed to rearrange the key to value mappings.
            – Tobi Alafin
            1 hour ago










          • You require all initial pairs to be disassociated in the result?
            – mickmackusa
            1 hour ago



















          3














          The swap() function is unecessary



          There's no need to create a swap() function. This can be done as a one-liner using native PHP. And, generally speaking, native functionality is going to be more performant than user-defined functions. It also means less code for you to write or maintain.



          list($a,$b) = [$b, $a];


          This takes two values, places them in an array and then using list() swaps them. In your case it would look like:



          list($arr[$keys[$i]],$arr[$keys[$target]]) = [$arr[$keys[$target]], $arr[$keys[$i]]];


          Friendly reminder: don't use short tags



          Short PHP tags (<?) has been discouraged for a long time. Although it is still supported it is disabled by default in the php.ini file and its use is discouraged. It sounds like it is not a big deal but this means having to make sure every time you set up an environment for this to run you have to make a special configuration change which is risky and time consuming and really shouldn't be necessary. (The short echo tag (<?=) is not discouraged and always available so feel free to use that as much as you like).




          PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).




          Source






          share|improve this answer










          New contributor




          John Conde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Oh my, the great John Conde comes to CodeReview. Welcome sir! I wonder what your opinion on php7 array deconstruction is.
            – mickmackusa
            16 hours ago






          • 1




            I like it. In fact, it is better than my proposed solution. I've been stuck in a PHP 5.6 world due to the size and complexity of my company's applications. But we're finishing up our (large and painful) PHP 7 migration so I'll get to start thinking in PHP 7 full time very soon!
            – John Conde
            15 hours ago










          • I don't know about great but I figured participating here will help me think creatively as I do less coding now than every before and I hope to keep my mind sharp. Or at least working.
            – John Conde
            15 hours ago










          • @JohnConde I'm aware that it is discouraged, and only use it for convenience in a testing/learning environment.
            – Tobi Alafin
            13 hours ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210790%2fshuffle-array-elements-by-rearranging-the-key-value-associations%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Rather than making iterated rand() calls, you should randomize the data once for best efficiency. To do this, just isolate the keys, shuffle them, then rejoin the values to the appropriate keys in the new order.



          Code: (Demo)



          function preserve_shuffle(&$arr)    #Shuffles the key-value associations in an array.
          {
          $keys = array_keys($arr); #extract the keys from the array.
          shuffle($keys);
          for ($index = 0, $length = count($keys); $index < $length; ++$index) {
          $result[$keys[$index]] = $arr[$keys[$index]];
          }
          $arr = $result;
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          preserve_shuffle($arr);
          var_export($arr);


          Or, if you prefer greater brevity or a functional syntax, you could use the following inside your custom function:



          $keys = array_keys($arr);   #extract the keys from the array.
          shuffle($keys);
          $arr = array_merge(array_flip($keys), $arr);


          Proof that it also works: https://3v4l.org/eLMo6





          My earlier snippets only reorder the associative data. The following will shuffle the associations without shuffling the key orders. It does not guarantee that all initially associations will be destroyed -- which I feel is beneficial / less predictable in a randomized result.



          function random_disassociate(&$assoc_array)
          {
          if (sizeof($assoc_array) < 2) {
          return; // data cannot be disassociated
          }
          $keys = array_keys($assoc_array);
          shuffle($assoc_array);
          $assoc_array = array_combine($keys, $assoc_array);
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          random_disassociate($arr);
          var_export($arr);





          share|improve this answer























          • Your function version doesn't work.
            – Tobi Alafin
            13 hours ago










          • That doesn't rearrange the key to value mappings. It just changes the order in which they are presented, but the key to value mappings are the same. The function is supposed to rearrange the key to value mappings.
            – Tobi Alafin
            1 hour ago










          • You require all initial pairs to be disassociated in the result?
            – mickmackusa
            1 hour ago
















          3














          Rather than making iterated rand() calls, you should randomize the data once for best efficiency. To do this, just isolate the keys, shuffle them, then rejoin the values to the appropriate keys in the new order.



          Code: (Demo)



          function preserve_shuffle(&$arr)    #Shuffles the key-value associations in an array.
          {
          $keys = array_keys($arr); #extract the keys from the array.
          shuffle($keys);
          for ($index = 0, $length = count($keys); $index < $length; ++$index) {
          $result[$keys[$index]] = $arr[$keys[$index]];
          }
          $arr = $result;
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          preserve_shuffle($arr);
          var_export($arr);


          Or, if you prefer greater brevity or a functional syntax, you could use the following inside your custom function:



          $keys = array_keys($arr);   #extract the keys from the array.
          shuffle($keys);
          $arr = array_merge(array_flip($keys), $arr);


          Proof that it also works: https://3v4l.org/eLMo6





          My earlier snippets only reorder the associative data. The following will shuffle the associations without shuffling the key orders. It does not guarantee that all initially associations will be destroyed -- which I feel is beneficial / less predictable in a randomized result.



          function random_disassociate(&$assoc_array)
          {
          if (sizeof($assoc_array) < 2) {
          return; // data cannot be disassociated
          }
          $keys = array_keys($assoc_array);
          shuffle($assoc_array);
          $assoc_array = array_combine($keys, $assoc_array);
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          random_disassociate($arr);
          var_export($arr);





          share|improve this answer























          • Your function version doesn't work.
            – Tobi Alafin
            13 hours ago










          • That doesn't rearrange the key to value mappings. It just changes the order in which they are presented, but the key to value mappings are the same. The function is supposed to rearrange the key to value mappings.
            – Tobi Alafin
            1 hour ago










          • You require all initial pairs to be disassociated in the result?
            – mickmackusa
            1 hour ago














          3












          3








          3






          Rather than making iterated rand() calls, you should randomize the data once for best efficiency. To do this, just isolate the keys, shuffle them, then rejoin the values to the appropriate keys in the new order.



          Code: (Demo)



          function preserve_shuffle(&$arr)    #Shuffles the key-value associations in an array.
          {
          $keys = array_keys($arr); #extract the keys from the array.
          shuffle($keys);
          for ($index = 0, $length = count($keys); $index < $length; ++$index) {
          $result[$keys[$index]] = $arr[$keys[$index]];
          }
          $arr = $result;
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          preserve_shuffle($arr);
          var_export($arr);


          Or, if you prefer greater brevity or a functional syntax, you could use the following inside your custom function:



          $keys = array_keys($arr);   #extract the keys from the array.
          shuffle($keys);
          $arr = array_merge(array_flip($keys), $arr);


          Proof that it also works: https://3v4l.org/eLMo6





          My earlier snippets only reorder the associative data. The following will shuffle the associations without shuffling the key orders. It does not guarantee that all initially associations will be destroyed -- which I feel is beneficial / less predictable in a randomized result.



          function random_disassociate(&$assoc_array)
          {
          if (sizeof($assoc_array) < 2) {
          return; // data cannot be disassociated
          }
          $keys = array_keys($assoc_array);
          shuffle($assoc_array);
          $assoc_array = array_combine($keys, $assoc_array);
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          random_disassociate($arr);
          var_export($arr);





          share|improve this answer














          Rather than making iterated rand() calls, you should randomize the data once for best efficiency. To do this, just isolate the keys, shuffle them, then rejoin the values to the appropriate keys in the new order.



          Code: (Demo)



          function preserve_shuffle(&$arr)    #Shuffles the key-value associations in an array.
          {
          $keys = array_keys($arr); #extract the keys from the array.
          shuffle($keys);
          for ($index = 0, $length = count($keys); $index < $length; ++$index) {
          $result[$keys[$index]] = $arr[$keys[$index]];
          }
          $arr = $result;
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          preserve_shuffle($arr);
          var_export($arr);


          Or, if you prefer greater brevity or a functional syntax, you could use the following inside your custom function:



          $keys = array_keys($arr);   #extract the keys from the array.
          shuffle($keys);
          $arr = array_merge(array_flip($keys), $arr);


          Proof that it also works: https://3v4l.org/eLMo6





          My earlier snippets only reorder the associative data. The following will shuffle the associations without shuffling the key orders. It does not guarantee that all initially associations will be destroyed -- which I feel is beneficial / less predictable in a randomized result.



          function random_disassociate(&$assoc_array)
          {
          if (sizeof($assoc_array) < 2) {
          return; // data cannot be disassociated
          }
          $keys = array_keys($assoc_array);
          shuffle($assoc_array);
          $assoc_array = array_combine($keys, $assoc_array);
          }

          $arr = ["a" => "apple", "b" => "banana", "c" => "capsicum", "d" => "dill"];
          random_disassociate($arr);
          var_export($arr);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 16 hours ago









          mickmackusa

          1,002112




          1,002112












          • Your function version doesn't work.
            – Tobi Alafin
            13 hours ago










          • That doesn't rearrange the key to value mappings. It just changes the order in which they are presented, but the key to value mappings are the same. The function is supposed to rearrange the key to value mappings.
            – Tobi Alafin
            1 hour ago










          • You require all initial pairs to be disassociated in the result?
            – mickmackusa
            1 hour ago


















          • Your function version doesn't work.
            – Tobi Alafin
            13 hours ago










          • That doesn't rearrange the key to value mappings. It just changes the order in which they are presented, but the key to value mappings are the same. The function is supposed to rearrange the key to value mappings.
            – Tobi Alafin
            1 hour ago










          • You require all initial pairs to be disassociated in the result?
            – mickmackusa
            1 hour ago
















          Your function version doesn't work.
          – Tobi Alafin
          13 hours ago




          Your function version doesn't work.
          – Tobi Alafin
          13 hours ago












          That doesn't rearrange the key to value mappings. It just changes the order in which they are presented, but the key to value mappings are the same. The function is supposed to rearrange the key to value mappings.
          – Tobi Alafin
          1 hour ago




          That doesn't rearrange the key to value mappings. It just changes the order in which they are presented, but the key to value mappings are the same. The function is supposed to rearrange the key to value mappings.
          – Tobi Alafin
          1 hour ago












          You require all initial pairs to be disassociated in the result?
          – mickmackusa
          1 hour ago




          You require all initial pairs to be disassociated in the result?
          – mickmackusa
          1 hour ago













          3














          The swap() function is unecessary



          There's no need to create a swap() function. This can be done as a one-liner using native PHP. And, generally speaking, native functionality is going to be more performant than user-defined functions. It also means less code for you to write or maintain.



          list($a,$b) = [$b, $a];


          This takes two values, places them in an array and then using list() swaps them. In your case it would look like:



          list($arr[$keys[$i]],$arr[$keys[$target]]) = [$arr[$keys[$target]], $arr[$keys[$i]]];


          Friendly reminder: don't use short tags



          Short PHP tags (<?) has been discouraged for a long time. Although it is still supported it is disabled by default in the php.ini file and its use is discouraged. It sounds like it is not a big deal but this means having to make sure every time you set up an environment for this to run you have to make a special configuration change which is risky and time consuming and really shouldn't be necessary. (The short echo tag (<?=) is not discouraged and always available so feel free to use that as much as you like).




          PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).




          Source






          share|improve this answer










          New contributor




          John Conde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Oh my, the great John Conde comes to CodeReview. Welcome sir! I wonder what your opinion on php7 array deconstruction is.
            – mickmackusa
            16 hours ago






          • 1




            I like it. In fact, it is better than my proposed solution. I've been stuck in a PHP 5.6 world due to the size and complexity of my company's applications. But we're finishing up our (large and painful) PHP 7 migration so I'll get to start thinking in PHP 7 full time very soon!
            – John Conde
            15 hours ago










          • I don't know about great but I figured participating here will help me think creatively as I do less coding now than every before and I hope to keep my mind sharp. Or at least working.
            – John Conde
            15 hours ago










          • @JohnConde I'm aware that it is discouraged, and only use it for convenience in a testing/learning environment.
            – Tobi Alafin
            13 hours ago
















          3














          The swap() function is unecessary



          There's no need to create a swap() function. This can be done as a one-liner using native PHP. And, generally speaking, native functionality is going to be more performant than user-defined functions. It also means less code for you to write or maintain.



          list($a,$b) = [$b, $a];


          This takes two values, places them in an array and then using list() swaps them. In your case it would look like:



          list($arr[$keys[$i]],$arr[$keys[$target]]) = [$arr[$keys[$target]], $arr[$keys[$i]]];


          Friendly reminder: don't use short tags



          Short PHP tags (<?) has been discouraged for a long time. Although it is still supported it is disabled by default in the php.ini file and its use is discouraged. It sounds like it is not a big deal but this means having to make sure every time you set up an environment for this to run you have to make a special configuration change which is risky and time consuming and really shouldn't be necessary. (The short echo tag (<?=) is not discouraged and always available so feel free to use that as much as you like).




          PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).




          Source






          share|improve this answer










          New contributor




          John Conde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Oh my, the great John Conde comes to CodeReview. Welcome sir! I wonder what your opinion on php7 array deconstruction is.
            – mickmackusa
            16 hours ago






          • 1




            I like it. In fact, it is better than my proposed solution. I've been stuck in a PHP 5.6 world due to the size and complexity of my company's applications. But we're finishing up our (large and painful) PHP 7 migration so I'll get to start thinking in PHP 7 full time very soon!
            – John Conde
            15 hours ago










          • I don't know about great but I figured participating here will help me think creatively as I do less coding now than every before and I hope to keep my mind sharp. Or at least working.
            – John Conde
            15 hours ago










          • @JohnConde I'm aware that it is discouraged, and only use it for convenience in a testing/learning environment.
            – Tobi Alafin
            13 hours ago














          3












          3








          3






          The swap() function is unecessary



          There's no need to create a swap() function. This can be done as a one-liner using native PHP. And, generally speaking, native functionality is going to be more performant than user-defined functions. It also means less code for you to write or maintain.



          list($a,$b) = [$b, $a];


          This takes two values, places them in an array and then using list() swaps them. In your case it would look like:



          list($arr[$keys[$i]],$arr[$keys[$target]]) = [$arr[$keys[$target]], $arr[$keys[$i]]];


          Friendly reminder: don't use short tags



          Short PHP tags (<?) has been discouraged for a long time. Although it is still supported it is disabled by default in the php.ini file and its use is discouraged. It sounds like it is not a big deal but this means having to make sure every time you set up an environment for this to run you have to make a special configuration change which is risky and time consuming and really shouldn't be necessary. (The short echo tag (<?=) is not discouraged and always available so feel free to use that as much as you like).




          PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).




          Source






          share|improve this answer










          New contributor




          John Conde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          The swap() function is unecessary



          There's no need to create a swap() function. This can be done as a one-liner using native PHP. And, generally speaking, native functionality is going to be more performant than user-defined functions. It also means less code for you to write or maintain.



          list($a,$b) = [$b, $a];


          This takes two values, places them in an array and then using list() swaps them. In your case it would look like:



          list($arr[$keys[$i]],$arr[$keys[$target]]) = [$arr[$keys[$target]], $arr[$keys[$i]]];


          Friendly reminder: don't use short tags



          Short PHP tags (<?) has been discouraged for a long time. Although it is still supported it is disabled by default in the php.ini file and its use is discouraged. It sounds like it is not a big deal but this means having to make sure every time you set up an environment for this to run you have to make a special configuration change which is risky and time consuming and really shouldn't be necessary. (The short echo tag (<?=) is not discouraged and always available so feel free to use that as much as you like).




          PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).




          Source







          share|improve this answer










          New contributor




          John Conde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited 18 hours ago





















          New contributor




          John Conde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 18 hours ago









          John Conde

          1786




          1786




          New contributor




          John Conde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          John Conde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          John Conde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • Oh my, the great John Conde comes to CodeReview. Welcome sir! I wonder what your opinion on php7 array deconstruction is.
            – mickmackusa
            16 hours ago






          • 1




            I like it. In fact, it is better than my proposed solution. I've been stuck in a PHP 5.6 world due to the size and complexity of my company's applications. But we're finishing up our (large and painful) PHP 7 migration so I'll get to start thinking in PHP 7 full time very soon!
            – John Conde
            15 hours ago










          • I don't know about great but I figured participating here will help me think creatively as I do less coding now than every before and I hope to keep my mind sharp. Or at least working.
            – John Conde
            15 hours ago










          • @JohnConde I'm aware that it is discouraged, and only use it for convenience in a testing/learning environment.
            – Tobi Alafin
            13 hours ago


















          • Oh my, the great John Conde comes to CodeReview. Welcome sir! I wonder what your opinion on php7 array deconstruction is.
            – mickmackusa
            16 hours ago






          • 1




            I like it. In fact, it is better than my proposed solution. I've been stuck in a PHP 5.6 world due to the size and complexity of my company's applications. But we're finishing up our (large and painful) PHP 7 migration so I'll get to start thinking in PHP 7 full time very soon!
            – John Conde
            15 hours ago










          • I don't know about great but I figured participating here will help me think creatively as I do less coding now than every before and I hope to keep my mind sharp. Or at least working.
            – John Conde
            15 hours ago










          • @JohnConde I'm aware that it is discouraged, and only use it for convenience in a testing/learning environment.
            – Tobi Alafin
            13 hours ago
















          Oh my, the great John Conde comes to CodeReview. Welcome sir! I wonder what your opinion on php7 array deconstruction is.
          – mickmackusa
          16 hours ago




          Oh my, the great John Conde comes to CodeReview. Welcome sir! I wonder what your opinion on php7 array deconstruction is.
          – mickmackusa
          16 hours ago




          1




          1




          I like it. In fact, it is better than my proposed solution. I've been stuck in a PHP 5.6 world due to the size and complexity of my company's applications. But we're finishing up our (large and painful) PHP 7 migration so I'll get to start thinking in PHP 7 full time very soon!
          – John Conde
          15 hours ago




          I like it. In fact, it is better than my proposed solution. I've been stuck in a PHP 5.6 world due to the size and complexity of my company's applications. But we're finishing up our (large and painful) PHP 7 migration so I'll get to start thinking in PHP 7 full time very soon!
          – John Conde
          15 hours ago












          I don't know about great but I figured participating here will help me think creatively as I do less coding now than every before and I hope to keep my mind sharp. Or at least working.
          – John Conde
          15 hours ago




          I don't know about great but I figured participating here will help me think creatively as I do less coding now than every before and I hope to keep my mind sharp. Or at least working.
          – John Conde
          15 hours ago












          @JohnConde I'm aware that it is discouraged, and only use it for convenience in a testing/learning environment.
          – Tobi Alafin
          13 hours ago




          @JohnConde I'm aware that it is discouraged, and only use it for convenience in a testing/learning environment.
          – Tobi Alafin
          13 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210790%2fshuffle-array-elements-by-rearranging-the-key-value-associations%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ellipse (mathématiques)

          Quarter-circle Tiles

          Mont Emei