{"id":2916,"date":"2020-03-19T17:31:07","date_gmt":"2020-03-20T00:31:07","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2916"},"modified":"2020-03-19T17:31:07","modified_gmt":"2020-03-20T00:31:07","slug":"randomizing-a-php-array","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2916","title":{"rendered":"Randomizing a PHP Array"},"content":{"rendered":"<p>I wrote this before it occurred to me to see if MariaDB could randomize the output. It can, with <code class=\"preserve-code-formatting\">ORDER BY RAND();<\/code>.<\/p>\n<p>PHP has a shuffle function that can randomize one array, but I have a bunch that need to have the same random order.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n\/\/ Create an array of numbers\n$randomArray = range(0,$numWords-1); \nshuffle($randomArray); \n\n\/\/ Create empty target arrays&nbsp;&nbsp;&nbsp;&nbsp; \n$word = []; $sounds = []; $phrase = []; $sentence = [];\n\nfor ( $i = 0; $i &lt; $numWords; $i++ ) {\n&nbsp;&nbsp;\/\/ Use the numbers in the random array to swap elements.\n&nbsp;&nbsp;$random = $randomArray[$i];\n&nbsp;&nbsp;$word[$i] = $word_S[$random];\n&nbsp;&nbsp;$sounds[$i] = $sounds_S[$random];\n&nbsp;&nbsp;$phrase[$i] = $phrase_S[$random];\n&nbsp;&nbsp;$sentence[$i] = $sentence_S[$random];\n}<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote this before it occurred to me to see if MariaDB could randomize the output. It can, with ORDER BY RAND();. PHP has a shuffle function that can randomize one array, but I have a bunch that need to have the same random order. \/\/ Create an array of numbers $randomArray = range(0,$numWords-1); shuffle($randomArray); &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2916\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Randomizing a PHP Array<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2916","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2916"}],"version-history":[{"count":4,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2916\/revisions"}],"predecessor-version":[{"id":2920,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2916\/revisions\/2920"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}