- strpos: documentation ( source)
- is_numeric: documentation ( source)
- substr_replace: documentation ( source)
- array_values: documentation ( source)
<?php
$str = 'UPDATE `list` set `item`=?,`type`=? WHERE (`id` = ?);';
$data = array(
'item' => '1',
'type' => 'Are you ok?'
);
$pos = 0;
$index = 0;
do {
$pos = strpos($str, '?', $pos);
if ($pos === false) {
break;
}
$binding = array_values($data)[$index%count($data)];
$replacement = is_numeric($binding) ? $binding : '"' . $binding . '"';
$str = substr_replace($str, $replacement, $pos, 1);
$pos += strlen($replacement);
$index++;
} while ($pos !== false);
echo $str;