Created
May 12, 2015 15:46
-
-
Save arabcoders/fc2e3429ba698d992429 to your computer and use it in GitHub Desktop.
This function is usefel if you want to use bind elements in PDO for IN clause for example: ( select * from table where id IN ( element, element) ... )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Create Binds for Array Elements. | |
* | |
* @param array $ids | |
* | |
* @return array | |
* @throws \InvalidArgumentException | |
*/ | |
function inArrayBind( array $ids ) | |
{ | |
if ( empty( $ids ) ) | |
{ | |
throw new \InvalidArgumentException( 'Array is empty.' ); | |
} | |
$token = md5( uniqid( rand(), true ) ); | |
$prefix = 'b' . substr( $token, 0, -( strlen( $token ) - 4 ) ) . 'd'; | |
$i = 0; | |
$bind = [ ]; | |
foreach ( $ids as $key => $value ) | |
{ | |
$i++; | |
$bind[$prefix . $i] = $value; | |
} | |
return [ | |
'bind' => $bind, | |
'queryString' => ':' . join( ', :', array_keys( $bind ) ) | |
]; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment