{"id":2078,"date":"2021-06-29T07:37:48","date_gmt":"2021-06-29T07:37:48","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2078"},"modified":"2025-04-06T04:56:53","modified_gmt":"2025-04-06T04:56:53","slug":"php-ksort","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-ksort\/","title":{"rendered":"PHP ksort"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PHP <code>ksort()<\/code> function to sort the keys of an associative array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-ksort-function'>Introduction to the PHP ksort() function <a href=\"#introduction-to-the-php-ksort-function\" class=\"anchor\" id=\"introduction-to-the-php-ksort-function\" title=\"Anchor for Introduction to the PHP ksort() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>ksort()<\/code> function sorts the elements of an array by their keys. The <code>ksort()<\/code> is mainly useful for sorting <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-associative-arrays\/\">associative arrays<\/a>.<\/p>\n\n\n\n<p>The following shows the syntax of the <code>ksort()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">ksort(<span class=\"hljs-keyword\">array<\/span> &amp;$array, int $flags = SORT_REGULAR): bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>ksort()<\/code> function has two parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$array<\/code> is the input array<\/li>\n\n\n\n<li><code>$flags<\/code> changes the sorting behavior using one or more values <code>SORT_REGULAR<\/code>, <code>SORT_NUMERIC<\/code>, <code>SORT_STRING<\/code>, <code>SORT_LOCALE_STRING<\/code>, <code>SORT_NATURAL<\/code>, and <code>SORT_FLAG_CASE<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>To combine the flag values, you use the | operator. For example, <code>SORT_STRING | SORT_NATURAL<\/code>.<\/p>\n\n\n\n<p>The <code>ksort()<\/code> function returns <code>true<\/code> on success or <code>false<\/code> on failure.<\/p>\n\n\n\n<p class=\"note\">Note that to sort the values of an array in ascending order, you use the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array-sort\/\">sort()<\/a><\/code> function instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-ksort-function-example'>PHP ksort() function example <a href=\"#php-ksort-function-example\" class=\"anchor\" id=\"php-ksort-function-example\" title=\"Anchor for PHP ksort() function example\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>ksort()<\/code> function to sort an associative array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$employees = &#91;\n    <span class=\"hljs-string\">'john'<\/span> =&gt; &#91;\n        <span class=\"hljs-string\">'age'<\/span> =&gt; <span class=\"hljs-number\">24<\/span>,\n        <span class=\"hljs-string\">'title'<\/span> =&gt; <span class=\"hljs-string\">'Front-end Developer'<\/span>\n    ],\n    <span class=\"hljs-string\">'alice'<\/span> =&gt; &#91;\n        <span class=\"hljs-string\">'age'<\/span> =&gt; <span class=\"hljs-number\">28<\/span>,\n        <span class=\"hljs-string\">'title'<\/span> =&gt; <span class=\"hljs-string\">'Web Designer'<\/span>\n    ],\n    <span class=\"hljs-string\">'bob'<\/span> =&gt; &#91;\n        <span class=\"hljs-string\">'age'<\/span> =&gt; <span class=\"hljs-number\">25<\/span>,\n        <span class=\"hljs-string\">'title'<\/span> =&gt; <span class=\"hljs-string\">'MySQL DBA'<\/span>\n    ]\n];\nksort($employees, SORT_STRING);\n\nprint_r($employees);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRlbXBsb3llZXMgPSBbCiAgICAnam9obicgPT4gWwogICAgICAgICdhZ2UnID0-IDI0LAogICAgICAgICd0aXRsZScgPT4gJ0Zyb250LWVuZCBEZXZlbG9wZXInCiAgICBdLAogICAgJ2FsaWNlJyA9PiBbCiAgICAgICAgJ2FnZScgPT4gMjgsCiAgICAgICAgJ3RpdGxlJyA9PiAnV2ViIERlc2lnbmVyJwogICAgXSwKICAgICdib2InID0-IFsKICAgICAgICAnYWdlJyA9PiAyNSwKICAgICAgICAndGl0bGUnID0-ICdNeVNRTCBEQkEnCiAgICBdCl07Cmtzb3J0KCRlbXBsb3llZXMsIFNPUlRfU1RSSU5HKTsKCnByaW50X3IoJGVtcGxveWVlcyk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define an associative array of employees with keys are the employee names.<\/li>\n\n\n\n<li>Second, use the <code>sort()<\/code> function to sort the keys of the <code>$employees<\/code> array in ascending order.<\/li>\n<\/ul>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;alice] =&gt; <span class=\"hljs-keyword\">Array<\/span>\n        (\n            &#91;age] =&gt; <span class=\"hljs-number\">28<\/span>\n            &#91;title] =&gt; Web Designer\n        )\n\n    &#91;bob] =&gt; <span class=\"hljs-keyword\">Array<\/span>\n        (\n            &#91;age] =&gt; <span class=\"hljs-number\">25<\/span>\n            &#91;title] =&gt; MySQL DBA\n        )\n\n    &#91;john] =&gt; <span class=\"hljs-keyword\">Array<\/span>\n        (\n            &#91;age] =&gt; <span class=\"hljs-number\">24<\/span>\n            &#91;title] =&gt; Front-end Developer\n        )\n\n)  <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='php-krsort-function'>PHP krsort() function <a href=\"#php-krsort-function\" class=\"anchor\" id=\"php-krsort-function\" title=\"Anchor for PHP krsort() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>krsort()<\/code> function is like the <code>ksort()<\/code> function except that it sorts the keys of an array in descending order:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">krsort(<span class=\"hljs-keyword\">array<\/span> &amp;$array, int $flags = SORT_REGULAR): bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>See the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$employees = &#91;\n    <span class=\"hljs-string\">'john'<\/span> =&gt; &#91;\n        <span class=\"hljs-string\">'age'<\/span> =&gt; <span class=\"hljs-number\">24<\/span>,\n        <span class=\"hljs-string\">'title'<\/span> =&gt; <span class=\"hljs-string\">'Front-end Developer'<\/span>\n    ],\n    <span class=\"hljs-string\">'alice'<\/span> =&gt; &#91;\n        <span class=\"hljs-string\">'age'<\/span> =&gt; <span class=\"hljs-number\">28<\/span>,\n        <span class=\"hljs-string\">'title'<\/span> =&gt; <span class=\"hljs-string\">'Web Designer'<\/span>\n    ],\n    <span class=\"hljs-string\">'bob'<\/span> =&gt; &#91;\n        <span class=\"hljs-string\">'age'<\/span> =&gt; <span class=\"hljs-number\">25<\/span>,\n        <span class=\"hljs-string\">'title'<\/span> =&gt; <span class=\"hljs-string\">'MySQL DBA'<\/span>\n    ]\n];\n\nkrsort($employees, SORT_STRING);\nprint_r($employees);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRlbXBsb3llZXMgPSBbCiAgICAnam9obicgPT4gWwogICAgICAgICdhZ2UnID0-IDI0LAogICAgICAgICd0aXRsZScgPT4gJ0Zyb250LWVuZCBEZXZlbG9wZXInCiAgICBdLAogICAgJ2FsaWNlJyA9PiBbCiAgICAgICAgJ2FnZScgPT4gMjgsCiAgICAgICAgJ3RpdGxlJyA9PiAnV2ViIERlc2lnbmVyJwogICAgXSwKICAgICdib2InID0-IFsKICAgICAgICAnYWdlJyA9PiAyNSwKICAgICAgICAndGl0bGUnID0-ICdNeVNRTCBEQkEnCiAgICBdCl07CgprcnNvcnQoJGVtcGxveWVlcywgU09SVF9TVFJJTkcpOwpwcmludF9yKCRlbXBsb3llZXMpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;john] =&gt; <span class=\"hljs-keyword\">Array<\/span>\n        (\n            &#91;age] =&gt; <span class=\"hljs-number\">24<\/span>\n            &#91;title] =&gt; Front-end Developer\n        )\n\n    &#91;bob] =&gt; <span class=\"hljs-keyword\">Array<\/span>\n        (\n            &#91;age] =&gt; <span class=\"hljs-number\">25<\/span>\n            &#91;title] =&gt; MySQL DBA\n        )\n\n    &#91;alice] =&gt; <span class=\"hljs-keyword\">Array<\/span>\n        (\n            &#91;age] =&gt; <span class=\"hljs-number\">28<\/span>\n            &#91;title] =&gt; Web Designer\n        )\n\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>ksort()<\/code> function to sort the keys of an associative array in ascending order.<\/li>\n\n\n\n<li>To sort the keys of an associative array, use the <code>krsort()<\/code> function.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2078\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-ksort\/\"\n\t\t\t\tdata-post-title=\"PHP ksort\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2078\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-ksort\/\"\n\t\t\t\tdata-post-title=\"PHP ksort\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PHP ksort() function to sort the keys of an associative array.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":58,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2078","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2078"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2078\/revisions"}],"predecessor-version":[{"id":3091,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2078\/revisions\/3091"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}