{"id":1268,"date":"2018-08-26T02:33:03","date_gmt":"2018-08-26T02:33:03","guid":{"rendered":"http:\/\/frowningbear.com\/codebase\/?p=1268"},"modified":"2019-01-30T13:37:02","modified_gmt":"2019-01-30T13:37:02","slug":"sorting-multi-dimensional-arrays-in-php","status":"publish","type":"post","link":"https:\/\/frowningbear.com\/codebase\/2018\/08\/26\/sorting-multi-dimensional-arrays-in-php\/","title":{"rendered":"Sorting multi-dimensional arrays in PHP"},"content":{"rendered":"<p>Some basic code examples.<\/p>\n\n\n<p><!--more--><\/p>\n\n\n<h3>Indexed array of indexed arrays<\/h3>\n<pre class=\"lang:default decode:true \">\/\/ Indexed array of indexed arrays\n$albums = array (\n    array( 'Toys in the Attic', \n\t       '1975', \n\t       'Aerosmith'\n\t),\n    array( 'Back in Black', \n\t       '1980', \n\t       'AC\/DC' ),\n    array( 'Paranoid', \n\t       '1970', \n\t       'Black Sabbath' \n\t\t  )\n);\n\nforeach ($albums as $key =&gt; $row) {\n    $artist[$key] = $row[2]; \t\n}\n\narray_multisort($artist, SORT_ASC, $albums);\n\nvar_dump($artist);\n\nforeach ($albums as $sub) {\n\tforeach ($sub as $k =&gt; $v) {\n\t\techo $k . \" - \" . $v . \"&lt;br&gt;\"; \n\t}\n\techo \"&lt;br&gt;\";\n}\n<\/pre>\n<h3>Indexed array of associative arrays<\/h3>\n<pre class=\"lang:default decode:true \">\/\/ Indexed array of associative arrays\n$albums = array(\n    array( 'album' =&gt; 'Toys in the Attic', \n           'year' =&gt; '1975', \n\t       'artist' =&gt; 'Aerosmith' \n\t\t  ),\n    array( 'album' =&gt; 'Back in Black', \n\t       'year' =&gt; '1980', \n\t       'artist' =&gt; 'AC\/DC'\n\t\t  ),\n    array( 'album' =&gt; 'Paranoid', \n\t       'year' =&gt; '1970', \n\t       'artist' =&gt; 'Black Sabbath' \n\t\t  )\n);\n\nforeach ($albums as $key =&gt; $row) {\n    $artist[$key] = $row['artist']; \t\n}\n\narray_multisort($artist, SORT_ASC, $albums);\n\nforeach ($albums as $sub) {\n\tforeach ($sub as $k =&gt; $v) {\n\t\techo $k . \" - \" . $v . \"&lt;br&gt;\"; \n\t}\n\techo \"&lt;br&gt;\";\n}<\/pre>\n<h3>Associative array of indexed arrays<\/h3>\n<pre class=\"lang:default decode:true \">\/\/ Associative array of indexed arrays\n$albums = array (\n    \"Toys in the Attic\"     =&gt; array( 'Toys in the Attic', \n\t                                  '1975', \n\t                                  'Aerosmith'\n\t                                ),\n    \"Back in Black\"         =&gt; array( 'Back in Black', \n\t                                  '1980', \n\t                                  'AC\/DC' \n\t\t\t\t\t\t\t        ),\n    \"Paranoid\"              =&gt; array( 'Paranoid', \n\t                                  '1970', \n\t                                  'Black Sabbath' \n\t\t                            )\n                );\n\nforeach ($albums as $key =&gt; $row) {\n    $album[$key] = $row[2]; \t\n}\n\narray_multisort($album, SORT_ASC, $albums);\n\nforeach ($albums as $sub) {\n\tforeach ($sub as $k =&gt; $v) {\n\t\techo $v . \"&lt;br&gt;\"; \n\t}\n\techo \"&lt;br&gt;\";\n}<\/pre>\n<h3>Associative array of associative arrays<\/h3>\n<pre class=\"lang:default decode:true \">\/\/ Associative array of associative arrays\n$albums = array (\n    \"Toys in the Attic\"     =&gt; array( 'title' =&gt; 'Toys in the Attic', \n\t                                  'year' =&gt; '1975', \n\t                                  'artist' =&gt; 'Aerosmith'\n\t                                ),\n    \"Back in Black\"         =&gt; array( 'title' =&gt; 'Back in Black', \n\t                                  'year' =&gt; '1980', \n\t                                  'artist' =&gt; 'AC\/DC' \n\t\t\t\t\t\t\t        ),\n    \"Paranoid\"              =&gt; array( 'title' =&gt; 'Paranoid', \n\t                                  'year' =&gt; '1970', \n\t                                  'artist' =&gt; 'Black Sabbath' \n\t\t                            )\n                );\n\nforeach ($albums as $key =&gt; $row) {\n    $album[$key] = $row['artist']; \t\n}\n\narray_multisort($album, SORT_ASC, $albums);\n\nforeach ($albums as $sub) {\n\tforeach ($sub as $k =&gt; $v) {\n\t\techo $v . \"&lt;br&gt;\"; \n\t}\n\techo \"&lt;br&gt;\";\n}<\/pre>","protected":false},"excerpt":{"rendered":"<p>Some basic code examples.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[51,52],"class_list":["post-1268","post","type-post","status-publish","format-standard","hentry","category-php","tag-array","tag-sort"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/1268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/comments?post=1268"}],"version-history":[{"count":3,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/1268\/revisions"}],"predecessor-version":[{"id":1343,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/1268\/revisions\/1343"}],"wp:attachment":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/media?parent=1268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/categories?post=1268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/tags?post=1268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}