darwin_tech Posted March 30, 2011 Share Posted March 30, 2011 Hi, I am converting a mysqli function to mysql SELECT strings. I basically have two arrays (I include with only one item a piece below for clarity). I want to replace the '?' in the first array values with the value in the second array. I know this should be an easy one and I'm missing something simple... Any help much appreciated. $parts = array('AND (taxon.Genus LIKE ?)'); $params = array(' wasmannia%'); $arrayCount = count($parts); for ($i = 0; $i < $arrayCount; ++$i) { str_replace('?', $params[$i], $parts[$i]); } Link to comment https://forums.phpfreaks.com/topic/232181-array-str_replace-problem/ Share on other sites More sharing options...
AbraCadaver Posted March 30, 2011 Share Posted March 30, 2011 Should work: $parts = array('AND (taxon.Genus LIKE ?)'); $params = array('wasmannia%'); $result = str_replace(array_fill(0, count($params), '?'), $params, $parts); Link to comment https://forums.phpfreaks.com/topic/232181-array-str_replace-problem/#findComment-1194394 Share on other sites More sharing options...
darwin_tech Posted March 30, 2011 Author Share Posted March 30, 2011 hey, that's a nice solution. I just got a version of my code to work (below). Thanks for the help - always better to have more than one solution $parts = array('AND (taxon.Genus LIKE ?)'); $params = array(' wasmannia%'); $arrayCount = count($parts); $mysqlParts = array(); for($i=0;$i<count($params);$i++) { $mysqlParts[] = str_replace("?",$params[$i],$parts[$i]); } Link to comment https://forums.phpfreaks.com/topic/232181-array-str_replace-problem/#findComment-1194401 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.