Skip to content

Instantly share code, notes, and snippets.

@arabcoders
Created May 12, 2015 15:46
Show Gist options
  • Save arabcoders/fc2e3429ba698d992429 to your computer and use it in GitHub Desktop.
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) ... )
<?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