{"id":7516,"date":"2023-02-27T17:19:37","date_gmt":"2023-02-27T17:19:37","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=7516"},"modified":"2023-02-28T09:24:06","modified_gmt":"2023-02-28T09:24:06","slug":"finding-the-biggest-number-in-an-array-of-objects-in-php","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/finding-the-biggest-number-in-an-array-of-objects-in-php\/","title":{"rendered":"Finding the Biggest Number in an Array of Objects in PHP"},"content":{"rendered":"\n<p>If you have an array of objects, such as products, you might want to find the biggest (or, conversly, smallest) number for some reason; you may want to show the biggest price so you can output &#8220;Prices Up To: \u00a3x&#8221; (or &#8220;Prices From: \u00a3x&#8221; with the smallest number). There are a couple of ways to do this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Approach 1: Iterating Through the Array<\/h2>\n\n\n\n<p>This is the obvious way to go before putting too much thought into it; iterate through the array in a loop and keep track of the biggest number seen so far, like this:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$biggest_number = 0;\nforeach($products as $product){\n  if($product-&gt;price &gt; $biggest_number){\n    $biggest_number = $product-&gt;price;\n  }\n}\necho &quot;The biggest number is: $biggest_number&quot;;<\/code><\/pre><\/div>\n\n\n\n<p>In this example, we use a <code>foreach<\/code> loop to iterate through the array and compare each number to the biggest number seen so far. This approach has a time complexity of O(n log n), where n is the size of the array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Approach 2: Using <code>max()<\/code> and <code>array_column()<\/code><\/h2>\n\n\n\n<p>A better way is to combine <code>max()<\/code> and <code>array_column()<\/code> functions to extract the maximum value. Here&#8217;s how:<code> <\/code><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$biggest_number = max(array_column($products, &quot;price&quot;));\necho &quot;The biggest number is: $biggest_number&quot;;<\/code><\/pre><\/div>\n\n\n\n<p>Here we use the <code>array_column()<\/code> function to extract the <code>price<\/code> column from an array of objects and then use the <code>max()<\/code> function on the new array. If you want to find the smallest number, simply replace <code>max()<\/code> with <code>min()<\/code>.<\/p>\n\n\n\n<p>This approach has a time complexity of O(n), where n is the size of the array. This makes it the most efficient of the two approaches, especially for large arrays.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have an array of objects, such as products, you might want to find the biggest (or, conversly, smallest) number for some reason; you may want to show the biggest price so you can output &#8220;Prices Up To: \u00a3x&#8221; (or &#8220;Prices From: \u00a3x&#8221; with the smallest number). There are a couple of ways to [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":7525,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-7516","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/7516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=7516"}],"version-history":[{"count":0,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/7516\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media\/7525"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=7516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=7516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}