{"id":220,"date":"2008-03-08T01:43:11","date_gmt":"2008-03-08T09:43:11","guid":{"rendered":"http:\/\/www.evardsson.com\/blog\/2008\/03\/08\/php-array_search-implicit-cast-of-search-term\/"},"modified":"2008-03-08T01:43:11","modified_gmt":"2008-03-08T09:43:11","slug":"php-array_search-implicit-cast-of-search-term","status":"publish","type":"post","link":"https:\/\/www.evardsson.com\/blog\/2008\/03\/08\/php-array_search-implicit-cast-of-search-term\/","title":{"rendered":"PHP array_search implicit cast of search term"},"content":{"rendered":"<p>There is an error in the values that array_search returns when searching on an array that has a mix of numeric values (123) and alpha-numeric mixed strings that start with the same and follow with alpha characters (&#8216;123a&#8217;).<\/p>\n<p>The results are actually kind of bizarre, but explainable by a bug in PHP&#8217;s equivalence test. When testing for equivalence (using ==) PHP determines that 123 == &#8216;123xyz&#8217;. PHP casts the string to an integer when doing the comparison (so &#8216;123xyz&#8217; becomes 123). This is documented in bugs.php.net (<a href=\"http:\/\/bugs.php.net\/bug.php?id=23110\">http:\/\/bugs.php.net\/bug.php?id=23110<\/a>) &#8211; but this leads to problems: both switch and array_search use == for comparison.<\/p>\n<p>So, using:<\/p>\n<pre class=\"brush: php\">\r\n$one = array (\r\n  'abc',\r\n  'abc1',\r\n  111,\r\n  '111b',\r\n  2,\r\n  '2xyz',\r\n  '123a',\r\n  123\r\n);\r\n$two = $one;\r\n\r\nforeach($one as $val)\r\n{\r\n  $key = array_search($val, $two);\r\n  if ($key !== false) {\r\n    echo \"$val == {$two[$key]} \\n\";\r\n    if (strcmp(strval($val), strval($two[$key])) == 0) {\r\n      echo \"strcmp returns true\";\r\n    } else {\r\n      echo \"strcmp returns false\";\r\n    }\r\n  } else {\r\n    echo \"$val not found \\n\";\r\n  }\r\n}\r\n<\/pre>\n<p>results in:<\/p>\n<pre>\r\nabc == abc -- strcmp returns true\r\nabc1 == abc1 -- strcmp returns true\r\n111 == 111 -- strcmp returns true\r\n111b == 111 -- strcmp returns false\r\n2 == 2 -- strcmp returns true\r\n2xyz == 2 -- strcmp returns false\r\n123a == 123a -- strcmp returns true\r\n123 == 123a -- strcmp returns false\r\n<\/pre>\n<p>This becomes a real problem when you can&#8217;t be sure that the values in an array are all of the same type. However, if you are sure that all the values in the array are of type string then array_search works flawlessly.<\/p>\n<p>I am still unsure how to work around this, however, I think having a version of array_search that doesn&#8217;t do an implicit cast on the search value would be of great use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is an error in the values that array_search returns when searching on an array that has a mix of numeric values (123) and alpha-numeric mixed strings that start with the same and follow with &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_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":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11,33],"tags":[148,170],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-development","category-php","tag-development","tag-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pxT7i-3y","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.evardsson.com\/blog\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.evardsson.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.evardsson.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.evardsson.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.evardsson.com\/blog\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":0,"href":"https:\/\/www.evardsson.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.evardsson.com\/blog\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.evardsson.com\/blog\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.evardsson.com\/blog\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}