{"id":2246,"date":"2013-02-08T08:15:18","date_gmt":"2013-02-08T08:15:18","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2246"},"modified":"2013-02-08T08:15:18","modified_gmt":"2013-02-08T08:15:18","slug":"php-find-value-in-array-how-to-find-value-in-array-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/php-find-value-in-array-how-to-find-value-in-array-examples\/","title":{"rendered":"Using PHP To Find Value In Array"},"content":{"rendered":"<p>Arrays are very important concept, be it any programming language. So while <a title=\"PHP Add To An Array | Add Element To Array Using PHP Examples\" href=\"https:\/\/theextremewebdesigns.com\/blog\/php-add-to-an-array-add-element-to-array-using-php-examples\/\" target=\"_blank\">working with arrays<\/a>, there might be a necessity to find a specific value in an array. In this article, I am going to share a very simple yet easy way to use \u00a0<em><strong>PHP to find value in array<\/strong><\/em>\u00a0by means of examples.<\/p>\n<h2>How to use PHP to Find Value In Array<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2246\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s assume that we have an array that contains fruits names. Now we would like to find if the value i.e. a specific fruit name exists in our array of fruits. PHP provides us with a very simple function called <strong>&#8220;array_search&#8221;<\/strong>. So <strong>&#8220;array_search&#8221;<\/strong> \u00a0searches an array for a given value and returns the corresponding key if they value is successfully found. All of the code is highly commented, so make sure you read each line to understand what each step actually does.<\/p>\n<h3>Example 1: By using array_search when array consists of Unique Values<\/h3>\n<pre class=\"lang:php decode:true\" title=\"Example 1\">&lt;?php \n\/\/Example 1: Declare an array with unique values===============================================\/\n$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple');\n\n\/\/Print the contents of the array. Use the &lt;pre&gt; tags to output the array in readable format.\necho 'Following are the original array contents:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array);\necho '&lt;\/pre&gt;';\n\n$key = array_search('grapes', $my_fruits_array);\n\n\/\/Echo the Key of the found value\necho 'The key of the grapes value is: '.$key;\n\necho '&lt;br \/&gt;&lt;br \/&gt;';\n?&gt;<\/pre>\n<h3>Example 2: By using array_search when array consists of Repeated\/Duplicate Values<\/h3>\n<pre class=\"lang:php decode:true\" title=\"Example 2\">&lt;?php \n\/\/Example 2: Declare an array with duplicate values. We will find index\/key of repeated values===\/\n$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple', 'grapes');\n\n\/\/Print the contents of the array. Use the &lt;pre&gt; tags to output the array in readable format.\necho 'Following are the array contents with duplicated values:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array);\necho '&lt;\/pre&gt;';\n\n\/\/Iterate through all values of our array\nforeach( $my_fruits_array as $key =&gt; $val )\n{\n\t\/\/If our desireds value is found, then simply store its key in a different array\n\tif( 'grapes' == $val )\n\t{\n\t\t$all_indices_for_value[] =  $key;\n\t}\n}\n\necho 'Print all indicies of the values found in array:&lt;br \/&gt;';\n\/\/Print all indicies of the values found in array\necho '&lt;pre&gt;';\nprint_r($all_indices_for_value);\necho '&lt;\/pre&gt;';\n\necho '&lt;br \/&gt;&lt;br \/&gt;';\n?&gt;<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2246\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p><strong>That&#8217;s it!<\/strong><br \/>\nDo you know of any other ways to use \u00a0<em><strong>PHP to find value in array<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Arrays are very important concept, be it any programming language. So while working with arrays, there might be a necessity to find a specific value in an array. In this article, I am going to share a very simple yet easy way to use \u00a0PHP to find value in array\u00a0by means of examples. How to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,13],"tags":[33],"class_list":["post-2246","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-tips-and-tricks","category-tips-and-tricks","tag-php-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-Ae","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=2246"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2246\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=2246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}