{"id":425,"date":"2022-10-27T11:48:41","date_gmt":"2022-10-27T11:48:41","guid":{"rendered":"https:\/\/www.devopstrainer.in\/blog\/?p=425"},"modified":"2022-10-27T11:48:41","modified_gmt":"2022-10-27T11:48:41","slug":"how-to-check-a-key-exist-in-an-array-explain-with-example","status":"publish","type":"post","link":"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/","title":{"rendered":"How to check a key exist in an array? explain with example"},"content":{"rendered":"\n<p>We have given an array arr and a Key key, the task is to check if a key exists in an array or not in PHP.<\/p>\n\n\n\n<p>Examples:<\/p>\n\n\n\n<p>Input : arr = [&#8220;Devop1&#8221;, &#8220;Devop2&#8221;, &#8220;1&#8221;, &#8220;2&#8221;,&#8221;3&#8243;]<br>key = &#8220;2&#8221;<br>Output: Found the Key<\/p>\n\n\n\n<p>Input : arr = [&#8220;Devop1&#8221;, &#8220;Devop2&#8221;, &#8220;1&#8221;, &#8220;2&#8221;,&#8221;3&#8243;]<br>key = 9<br>Output: Key not Found<\/p>\n\n\n\n<p>The problem can be solved using PHP inbuilt function for checking key exists in a given array. The in-built function used for the given problem are:<\/p>\n\n\n\n<p>Method 1: <strong>Using array_key_exists() Method:<\/strong> The array_key_exists() function checks whether a specific key or index is present inside an array or not.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<p>boolean array_key_exists( $index, $array )<br>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\/\/ PHP program to check if a key \r\n\/\/ exists in an array or not\r\n  \r\n$array = array(\r\n      'names' => array(\"Devop1\", \"Devop2\", \"Devop3\"), \r\n    'rank' => array('1', '2', '3')\r\n); \r\n  \r\n\/\/ Use of array_key_exists() function\r\nif(array_key_exists(\"rank\", $array)) {\r\n    echo \"Found the Key\"; \r\n} \r\nelse{ \r\n    echo \"Key not Found\"; \r\n} \r\n  \r\n?><\/code><\/pre>\n\n\n\n<p>Output<br>Found the Key<br>Method 2: <strong>Using isset() Method:<\/strong> The isset() function checks whether a specific key or index is present inside an array or not.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<p>bool isset( mixed $var, mixed $\u2026 )<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\/\/ PHP program to check if a key \r\n\/\/ exists in an array or not\r\n  \r\n$array = array(\r\n      'names' => array(\"Devop1\", \"Devop2\", \"Devop3\"), \r\n    'rank' => array('1', '2', '3')\r\n); \r\n  \r\n\/\/ Use of array_key_exists() function\r\nif(isset($array&#91;\"rank\"])){\r\n    echo \"Found the Key\"; \r\n} \r\nelse{ \r\n    echo \"Key not Found\"; \r\n} \r\n  \r\n?><\/code><\/pre>\n\n\n\n<p>Output<br>Found the Key<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have given an array arr and a Key key, the task is to check if a key exists in an array or not in PHP. Examples:&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145,165,113],"tags":[173,175,176,174],"class_list":["post-425","post","type-post","status-publish","format-standard","hentry","category-array","category-function","category-php","tag-array_key_exists-method","tag-check-a-key-exist-in-an-array","tag-php-inbuilt-function","tag-using-isset-method"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to check a key exist in an array? explain with example - DevOps | SRE | DevSecOps<\/title>\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.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to check a key exist in an array? explain with example - DevOps | SRE | DevSecOps\" \/>\n<meta property=\"og:description\" content=\"We have given an array arr and a Key key, the task is to check if a key exists in an array or not in PHP. Examples:...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps | SRE | DevSecOps\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-27T11:48:41+00:00\" \/>\n<meta name=\"author\" content=\"rahulkr kr\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rahulkr kr\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-check-a-key-exist-in-an-array-explain-with-example\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-check-a-key-exist-in-an-array-explain-with-example\\\/\"},\"author\":{\"name\":\"rahulkr kr\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\"},\"headline\":\"How to check a key exist in an array? explain with example\",\"datePublished\":\"2022-10-27T11:48:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-check-a-key-exist-in-an-array-explain-with-example\\\/\"},\"wordCount\":148,\"commentCount\":0,\"keywords\":[\"array_key_exists() Method\",\"check a key exist in an array\",\"PHP inbuilt function\",\"Using isset() Method\"],\"articleSection\":[\"Array\",\"Function\",\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-check-a-key-exist-in-an-array-explain-with-example\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-check-a-key-exist-in-an-array-explain-with-example\\\/\",\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-check-a-key-exist-in-an-array-explain-with-example\\\/\",\"name\":\"How to check a key exist in an array? explain with example - DevOps | SRE | DevSecOps\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#website\"},\"datePublished\":\"2022-10-27T11:48:41+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-check-a-key-exist-in-an-array-explain-with-example\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-check-a-key-exist-in-an-array-explain-with-example\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-check-a-key-exist-in-an-array-explain-with-example\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to check a key exist in an array? explain with example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/\",\"name\":\"DevOps | SRE | DevSecOps\",\"description\":\"Automation means Cost, Quality, Time\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\",\"name\":\"rahulkr kr\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g\",\"caption\":\"rahulkr kr\"},\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/author\\\/rahulkr\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to check a key exist in an array? explain with example - DevOps | SRE | DevSecOps","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.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/","og_locale":"en_US","og_type":"article","og_title":"How to check a key exist in an array? explain with example - DevOps | SRE | DevSecOps","og_description":"We have given an array arr and a Key key, the task is to check if a key exists in an array or not in PHP. Examples:...","og_url":"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/","og_site_name":"DevOps | SRE | DevSecOps","article_published_time":"2022-10-27T11:48:41+00:00","author":"rahulkr kr","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rahulkr kr","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/#article","isPartOf":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/"},"author":{"name":"rahulkr kr","@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d"},"headline":"How to check a key exist in an array? explain with example","datePublished":"2022-10-27T11:48:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/"},"wordCount":148,"commentCount":0,"keywords":["array_key_exists() Method","check a key exist in an array","PHP inbuilt function","Using isset() Method"],"articleSection":["Array","Function","PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/","url":"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/","name":"How to check a key exist in an array? explain with example - DevOps | SRE | DevSecOps","isPartOf":{"@id":"https:\/\/www.devopstrainer.in\/blog\/#website"},"datePublished":"2022-10-27T11:48:41+00:00","author":{"@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d"},"breadcrumb":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-check-a-key-exist-in-an-array-explain-with-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopstrainer.in\/blog\/"},{"@type":"ListItem","position":2,"name":"How to check a key exist in an array? explain with example"}]},{"@type":"WebSite","@id":"https:\/\/www.devopstrainer.in\/blog\/#website","url":"https:\/\/www.devopstrainer.in\/blog\/","name":"DevOps | SRE | DevSecOps","description":"Automation means Cost, Quality, Time","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopstrainer.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d","name":"rahulkr kr","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g","caption":"rahulkr kr"},"url":"https:\/\/www.devopstrainer.in\/blog\/author\/rahulkr\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/comments?post=425"}],"version-history":[{"count":2,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/425\/revisions"}],"predecessor-version":[{"id":427,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/425\/revisions\/427"}],"wp:attachment":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/media?parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/categories?post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/tags?post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}