PHP Faster than `in_array`

<?php /** * ------------------------------------------------------------------------------------------------------------------- * https://github.com/mecha-cms/mecha-cms/blob/1e0d4518bc5546714a6052f6a6fdad0490d8d5cc/engine/kernel/mecha.php#L106 * ------------------------------------------------------------------------------------------------------------------- */ $x = "\x1A"; // any character(s) that is impossible to be available in the array value $bucket = array('foo', 'bar', 'baz', 'qux'); // lots of array data $item = 'foo'; // check this item // Check if `$bucket` contains `$item` ... if(strpos($x . implode($x, $bucket) . $x, $x . $item . $x) !== false) { echo 'Yes it is!'; }
This replaces `if(in_array($item, $bucket)) { ... }`

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.