{"id":3142,"date":"2020-08-18T09:58:40","date_gmt":"2020-08-18T08:58:40","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3142"},"modified":"2023-04-28T11:57:19","modified_gmt":"2023-04-28T10:57:19","slug":"search-array","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/memory\/arrays\/search-array","title":{"rendered":"Search array"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Does value exist in array<\/h4>\n\n\n\n<p>in_array() checks if the specified value exits in the array. &lt;&lt;Best for value matching test<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  if (in_array(\"MySearchValue\", $MyArray, True))\n    \/\/Do something\n\n  if (!in_array(\"MySearchValue\", $MyArray, True))\n    \/\/Do something<\/code><\/pre>\n\n\n\n<p>array_search() searches an array for a given value and returns the key, or False<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  if (array_search(\"MySearchValue\", $MyArray, True) !== False)\n    \/\/Do something<\/code><\/pre>\n\n\n\n<p><em>Note that in_array() is the better choice if you are value matching within an array, because you can test for a True result.  array_search() can return a not False result in error conditions<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Search a simple array 1 dimensional array<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  if (array_search($NewValue, $MyArray) === False)\n  {\n    \/\/Does not exist\n    $MyArray&#91;] = $NewValue;\n  }\n\n  \/\/To test if true (returns index if True, so use not false)\n  if (array_search($NewValue, $MyArray) !== False)\n      \n  \/\/For a nested array\n  if (array_search($NewValue, $MyArray&#91;$MyKey]&#91;'SomeField']) === False)\n  {\n    \/\/Does not exist\n    $MyArray&#91;$MyKey]&#91;'SomeField']&#91;] = $NewValue;\n  }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">#Does a field value exist within a multi dimensional array?<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyMultiDimArray = array(    \n    0 =&gt;  array(  \n      \"id\" =&gt;  14,\n      \"name\" =&gt; \"Adam\",\n    ),  \n    1   =&gt;  array(  \n        \"id\" =&gt; 2032,\n        \"name\" =&gt; \"James\"\n    ), \n    2   =&gt;  array(  \n        \"id\" =&gt; 18,\n        \"name\" =&gt; \"Jane\"\n    )\n  );\n\n\n  \/\/Simple exists or not\n  if (array_search(2032, array_column($MyMultiDimArray, 'id')) !== False)\n    echo 'FOUND!';\n  else\n    echo 'NOT FOUND!';\n\n\n  \/\/Get key if it exists - looking for match\n  $Key = array_search(2032, array_column($MyMultiDimArray, 'id'));\n  if ($Key !== False)\n  {\n    $Name = $MyMultiDimArray&#91;$Key]&#91;'name'];\n  }\n\n\n  \/\/Get key if it exists  - create if not exist\n  $Key = array_search(2032, array_column($MyMultiDimArray, 'id'));\n  if ($Key === False)\n  {\n    $MyMultiDimArray&#91;]&#91;'id'] = 2032;\n    $Key = count($MyMultiDimArray) - 1;\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Replace value in array<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyArray&#91;array_search(\"Old value\", $MyArray)] = \"New Value\";<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Does value exist in array in_array() checks if the specified value exits in the array. &lt;&lt;Best for value matching test array_search() searches an array for a given value and returns the key, or False Note that in_array() is the better choice if you are value matching within an array, because you can test for a [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-3142","post","type-post","status-publish","format-standard","hentry","category-arrays"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3142"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3142\/revisions"}],"predecessor-version":[{"id":4496,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3142\/revisions\/4496"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}