{"id":1972,"date":"2015-02-16T18:51:30","date_gmt":"2015-02-17T02:51:30","guid":{"rendered":"https:\/\/www.virendrachandak.com\/techtalk\/?p=1972"},"modified":"2015-03-05T16:37:33","modified_gmt":"2015-03-06T00:37:33","slug":"how-to-sort-a-multi-dimension-array-by-value-in-php","status":"publish","type":"post","link":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/","title":{"rendered":"How to sort a multi-dimension array by value in PHP"},"content":{"rendered":"<p>In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this. One way to to use <a href=\"http:\/\/php.net\/manual\/en\/function.usort.php\" title=\"PHP: usort - Manual\" rel=\"external nofollow\">usort()<\/a> function. Another way is to just identify the values and create another array with the values and then use it in the <a href=\"http:\/\/php.net\/manual\/en\/function.array-multisort.php\" title=\"PHP: usort - Manual\" rel=\"external nofollow\">array_multisort()<\/a> function. Using the multisort method we can easily sort a multi-dimension array based on its one or more values. Lets see how we can use both these methods.<br \/>\n<!--more--><br \/>\nYou can view the demo of all these methods <a id=\"demo_link\" data-title=\"View Demo\" href=\"https:\/\/www.virendrachandak.com\/demos\/how-to-sort-a-multi-dimension-array-by-value-in-php.php\" target=\"_blank\">here<\/a>.<\/p>\n<h3>Sort using usort<\/h3>\n<p>The first method to sort the array is by using the usort() function. Here is the code that we can use to perform this sort:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction cmp($a, $b)\r\n{\r\n\treturn strcmp($a&#x5B;&quot;name&quot;], $b&#x5B;&quot;name&quot;]);\r\n}\r\nusort($vc_array, &quot;cmp&quot;);\r\n<\/pre>\n<p><a id=\"demo_link\" data-title=\"View Output\" href=\"https:\/\/www.virendrachandak.com\/demos\/how-to-sort-a-multi-dimension-array-by-value-in-php.php#usort\" target=\"_blank\"><i class=\"icon fa fa-camera-retro\"><\/i>View Output<\/a><\/p>\n<p>The usort() function sorts the <code>$vc_array<\/code> by using the comparison function (<code>cmp<\/code>) that we created. The above code can be used to sort the multi-dimension array based on the value of the name column of the array. This is an easy way to sort the multi-dimension array based on the value of one keys. <\/p>\n<p>If you want to sort the array based on values of multiple keys then, you might have to write some complex logic in the callback function to do that. However, there is an alternative way by using the array_multisort() function. The array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. <\/p>\n<h3>Sort using array_multisort by value of 1 key<\/h3>\n<p>Lets now see how to use the array_multisort() function to do the same sorting as the one we did using usort above.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nforeach ($vc_array as $key =&gt; $row)\r\n{\r\n\t$vc_array_name&#x5B;$key] = $row&#x5B;'name'];\r\n}\r\narray_multisort($vc_array_name, SORT_ASC, $vc_array);\r\n<\/pre>\n<p><a id=\"demo_link\" data-title=\"View Output\" href=\"https:\/\/www.virendrachandak.com\/demos\/how-to-sort-a-multi-dimension-array-by-value-in-php.php#multisort_1key\" target=\"_blank\"><i class=\"icon fa fa-camera-retro\"><\/i>View Output<\/a><\/p>\n<p>In the above code we first create a new array <code>$vc_array_name<\/code> to store all the values that we want to sort the <code>$vc_array<\/code> by. Then we use the array_multisort() to sort the arrays. In this function we first sort the <code>$vc_array_name<\/code> ascending and then sort the <code>$vc_array<\/code>.<\/p>\n<h3>Sort using array_multisort by value of 2 keys<\/h3>\n<p>Now, lets see how we can sort the same array by values of 2 keys of the array. In this example, we will sort by value ascending, name descending.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nforeach ($vc_array as $key =&gt; $row)\r\n{\r\n\t$vc_array_value&#x5B;$key] = $row&#x5B;'value'];\r\n\t$vc_array_name&#x5B;$key] = $row&#x5B;'name'];\r\n}\r\narray_multisort($vc_array_value, SORT_ASC, $vc_array_name, SORT_DESC, $vc_array);\r\n<\/pre>\n<p><a id=\"demo_link\" data-title=\"View Output\" href=\"https:\/\/www.virendrachandak.com\/demos\/how-to-sort-a-multi-dimension-array-by-value-in-php.php#multisort_2keys\" target=\"_blank\"><i class=\"icon fa fa-camera-retro\"><\/i>View Output<\/a><\/p>\n<p>This code first sorts the <code>$vc_array_values<\/code> ascending, <code>$vc_array_name<\/code> descending and then the <code>$vc_array<\/code> using both those sorted arrays. Using this method we can sort the multi-dimension array depending on values of multiple different keys.<\/p>\n<h3>Sort using array_multisort by value of 3 keys<\/h3>\n<p>Now, lets see how we can sort the same array by values of 3 keys of the array. In this example, we will sort by value descending, order descending and name ascending.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nforeach ($vc_array as $key =&gt; $row)\r\n{\r\n\t$vc_array_value&#x5B;$key] = $row&#x5B;'value'];\r\n\t$vc_array_name&#x5B;$key] = $row&#x5B;'name'];\r\n\t$vc_array_order&#x5B;$key] = $row&#x5B;'order'];\r\n}\r\narray_multisort($vc_array_value, SORT_DESC, $vc_array_order, SORT_DESC, $vc_array_name, SORT_ASC, $vc_array);\r\n<\/pre>\n<p><a id=\"demo_link\" data-title=\"View Output\" href=\"https:\/\/www.virendrachandak.com\/demos\/how-to-sort-a-multi-dimension-array-by-value-in-php.php#multisort_3keys\" target=\"_blank\"><i class=\"icon fa fa-camera-retro\"><\/i>View Output<\/a><\/p>\n<p>This code first sorts the <code>$vc_array_values<\/code> descending,  <code>$vc_array_order<\/code> descending, <code>$vc_array_name<\/code> ascending and then the <code>$vc_array<\/code> using these sorted arrays. Using this method we can sort the multi-dimension array depending on values of multiple different keys. This method can be extended to sort an array by any number of values.<\/p>\n<p><a id=\"demo_link\" data-title=\"View All Demo\" href=\"https:\/\/www.virendrachandak.com\/demos\/how-to-sort-a-multi-dimension-array-by-value-in-php.php\" target=\"_blank\"><i class=\"icon fa fa-camera-retro\"><\/i>View All Demo<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this. One way to to use usort() function. Another way is to just identify the values and create another array with the values and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[143,8],"tags":[124,140],"class_list":["post-1972","post","type-post","status-publish","format-standard","hentry","category-php","category-web-development","tag-snippets","tag-sorting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to sort a multi-dimension array by value in PHP - Virendra&#039;s TechTalk<\/title>\n<meta name=\"description\" content=\"In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to sort a multi-dimension array by value in PHP - Virendra&#039;s TechTalk\" \/>\n<meta property=\"og:description\" content=\"In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Virendra&#039;s TechTalk\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:published_time\" content=\"2015-02-17T02:51:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-03-06T00:37:33+00:00\" \/>\n<meta name=\"author\" content=\"Virendra Chandak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:site\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virendra Chandak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-sort-a-multi-dimension-array-by-value-in-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-sort-a-multi-dimension-array-by-value-in-php\\\/\"},\"author\":{\"name\":\"Virendra Chandak\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"headline\":\"How to sort a multi-dimension array by value in PHP\",\"datePublished\":\"2015-02-17T02:51:30+00:00\",\"dateModified\":\"2015-03-06T00:37:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-sort-a-multi-dimension-array-by-value-in-php\\\/\"},\"wordCount\":624,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"keywords\":[\"snippets\",\"sorting\"],\"articleSection\":[\"PHP\",\"Web Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-sort-a-multi-dimension-array-by-value-in-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-sort-a-multi-dimension-array-by-value-in-php\\\/\",\"url\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-sort-a-multi-dimension-array-by-value-in-php\\\/\",\"name\":\"How to sort a multi-dimension array by value in PHP - Virendra&#039;s TechTalk\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#website\"},\"datePublished\":\"2015-02-17T02:51:30+00:00\",\"dateModified\":\"2015-03-06T00:37:33+00:00\",\"description\":\"In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-sort-a-multi-dimension-array-by-value-in-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-sort-a-multi-dimension-array-by-value-in-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-sort-a-multi-dimension-array-by-value-in-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"TechTalk\",\"item\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/category\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to sort a multi-dimension array by value in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#website\",\"url\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/\",\"name\":\"Virendra's TechTalk\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\",\"name\":\"Virendra Chandak\",\"logo\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.virendrachandak.com\",\"https:\\\/\\\/www.facebook.com\\\/virendrachandak\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/virendrachandak\\\/\",\"https:\\\/\\\/x.com\\\/virendrachandak\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to sort a multi-dimension array by value in PHP - Virendra&#039;s TechTalk","description":"In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/","og_locale":"en_US","og_type":"article","og_title":"How to sort a multi-dimension array by value in PHP - Virendra&#039;s TechTalk","og_description":"In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this.","og_url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/","og_site_name":"Virendra&#039;s TechTalk","article_publisher":"https:\/\/www.facebook.com\/virendrachandak","article_author":"https:\/\/www.facebook.com\/virendrachandak","article_published_time":"2015-02-17T02:51:30+00:00","article_modified_time":"2015-03-06T00:37:33+00:00","author":"Virendra Chandak","twitter_card":"summary_large_image","twitter_creator":"@virendrachandak","twitter_site":"@virendrachandak","twitter_misc":{"Written by":"Virendra Chandak","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/#article","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/"},"author":{"name":"Virendra Chandak","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"headline":"How to sort a multi-dimension array by value in PHP","datePublished":"2015-02-17T02:51:30+00:00","dateModified":"2015-03-06T00:37:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/"},"wordCount":624,"commentCount":0,"publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"keywords":["snippets","sorting"],"articleSection":["PHP","Web Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/","url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/","name":"How to sort a multi-dimension array by value in PHP - Virendra&#039;s TechTalk","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website"},"datePublished":"2015-02-17T02:51:30+00:00","dateModified":"2015-03-06T00:37:33+00:00","description":"In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this.","breadcrumb":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"TechTalk","item":"https:\/\/www.virendrachandak.com\/techtalk\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},{"@type":"ListItem","position":3,"name":"How to sort a multi-dimension array by value in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website","url":"https:\/\/www.virendrachandak.com\/techtalk\/","name":"Virendra's TechTalk","description":"","publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.virendrachandak.com\/techtalk\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17","name":"Virendra Chandak","logo":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.virendrachandak.com","https:\/\/www.facebook.com\/virendrachandak","https:\/\/www.linkedin.com\/in\/virendrachandak\/","https:\/\/x.com\/virendrachandak"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2vTtQ-vO","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1086,"url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/","url_meta":{"origin":1972,"position":0},"title":"How to apply a function to every array element in PHP","author":"Virendra Chandak","date":"October 21, 2012","format":false,"excerpt":"I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input, function like htmlentities can be used. We can easily use this function on a single variable or a single dimension array by\u2026","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1714,"url":"https:\/\/www.virendrachandak.com\/techtalk\/php-5-5-password-hashing-api\/","url_meta":{"origin":1972,"position":1},"title":"PHP 5.5 Password Hashing API","author":"Virendra Chandak","date":"June 1, 2014","format":false,"excerpt":"Most of the applications or websites today have a user registration system which requires storing usernames, passwords etc. A developer of the application should always store passwords securely and never in plain text. There are many methods to encrypt or hash passwords and store in the database but which method\u2026","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1163,"url":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/","url_meta":{"origin":1972,"position":2},"title":"MySQL ordering results by specific field values","author":"Virendra Chandak","date":"September 16, 2012","format":false,"excerpt":"In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort the results in a specific order which cannot be done using the ASC or DSC. FIELD() of MySQL ORDER BY clause\u2026","rel":"","context":"In &quot;MySQL&quot;","block_context":{"text":"MySQL","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2441,"url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-track-web-vitals-in-google-analytics\/","url_meta":{"origin":1972,"position":3},"title":"Tracking Web Vitals in Google Analytics","author":"Virendra Chandak","date":"June 3, 2020","format":false,"excerpt":"There are a few different ways to track Web Vitals, however the easiest way is using the web-vitals JavaScript library. In this post we will load the web-vitals directly from CDN and use that to track the Core Web Vitals (LCP, FID, CLS) and other Web Vitals (FCP, TTFB) metrics.\u2026","rel":"","context":"In &quot;Optimization Tips&quot;","block_context":{"text":"Optimization Tips","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/optimization-tips\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1673,"url":"https:\/\/www.virendrachandak.com\/techtalk\/using-php-create-passwords-for-htpasswd-file\/","url_meta":{"origin":1972,"position":4},"title":"How to generate passwords for .htpasswd using PHP","author":"Virendra Chandak","date":"March 2, 2014","format":false,"excerpt":"In my earlier post about .htaccess I had described about authentication using .htaccess and command to generate .htpasswd file. However, when we want to add passwords for many users that method will take too long, since we will have to add passwords for each user one at a time. However,\u2026","rel":"","context":"In &quot;Security&quot;","block_context":{"text":"Security","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/security\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":450,"url":"https:\/\/www.virendrachandak.com\/techtalk\/getting-real-client-ip-address-in-php-2\/","url_meta":{"origin":1972,"position":5},"title":"Getting real client IP address in PHP","author":"Virendra Chandak","date":"October 23, 2011","format":false,"excerpt":"Many times we need the visitor's ipaddress for validation, security, spam prevention, etc. Getting the Visitor's ipaddress is very easy in PHP. The simplest way to get the visitor's\/client's ipaddress is using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables. The variable in the $_SERVER array are created by the web server (like\u2026","rel":"","context":"In &quot;Functionality&quot;","block_context":{"text":"Functionality","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/functionality\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/comments?post=1972"}],"version-history":[{"count":0,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1972\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/media?parent=1972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/categories?post=1972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/tags?post=1972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}