{"id":1596,"date":"2015-05-12T06:00:54","date_gmt":"2015-05-12T06:00:54","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1596"},"modified":"2015-08-03T06:08:40","modified_gmt":"2015-08-03T06:08:40","slug":"merge-two-sorted-arrays-and-the-resulting-array-should-also-be-sorted","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/merge-two-sorted-arrays-and-the-resulting-array-should-also-be-sorted\/","title":{"rendered":"Merge two sorted arrays and the resulting array should also be sorted"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[1596]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/5064532\/merge-two-sorted-arrays-and-the-resulting-array-should-also-be-sorted\">question<\/a> was:<\/p>\n<p>Let&#8217;s say you have two arrays of arrays with the same structure but different count of arrays in them:<\/p>\n<pre class=\"lang:default decode:true\">$arr1 = array(array(1,\"b\"), array(2,\"a\"), array(5,\"c\"));  \r\n$arr2 = array(array(3,\"e\"));<\/pre>\n<p>Now, the data in the $arr1 and $arr2 is sorted, and now what I would like it to merge these two arrays, so I did this:<\/p>\n<pre class=\"lang:default decode:true\">$res = array_merge($arr1, $arr2);<\/pre>\n<p>And then I get an output like this:<\/p>\n<pre class=\"lang:default decode:true\">1-b  \r\n2-a\r\n5-c  \r\n3-e<\/pre>\n<p>But, I would like to have a sorted $res also like this:<\/p>\n<pre class=\"lang:default decode:true\">1-b  \r\n2-a\r\n3-e  \r\n5-c<\/pre>\n<p>I wonder if there&#8217;s a function in PHP to do this automatically, without me having to write my own function? Or, please advise me on which is the best approach for this if I want to (later on) add sorting by the next parameter so the output would be like this<\/p>\n<pre class=\"lang:default decode:true\">2-a  \r\n1-b\r\n5-c  \r\n3-e<\/pre>\n<p>&nbsp;<\/p>\n<p>The answer, by user <a href=\"http:\/\/stackoverflow.com\/users\/615032\/fieg\">fieg<\/a>, was:<\/p>\n<blockquote><p>You can first merge the arrays and then sort the final array.<\/p>\n<p>You are probably looking for a multi-sort function. I usually use this function (I found this functions somewhere on the internet years ago, credits go to the original author):<\/p>\n<pre class=\"lang:default decode:true  \">\/*\r\n * sort a multi demensional array on a column\r\n *\r\n * @param array $array array with hash array\r\n * @param mixed $column key that you want to sort on\r\n * @param enum $order asc or desc\r\n *\/\r\nfunction array_qsort2 (&amp;$array, $column=0, $order=\"ASC\") {\r\n    $oper = ($order == \"ASC\")?\"&gt;\":\"&lt;\";\r\n    if(!is_array($array)) return;\r\n    usort($array, create_function('$a,$b',\"return (\\$a['$column'] $oper \\$b['$column']);\")); \r\n    reset($array);\r\n}<\/pre>\n<p>You can use it like this:<\/p>\n<pre class=\"lang:default decode:true\">array_qsort2($res, 0, \"ASC\");<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1596","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1596"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1596\/revisions"}],"predecessor-version":[{"id":1680,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1596\/revisions\/1680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}