sweetpea Posted September 15, 2008 Share Posted September 15, 2008 Hello everyone, I populate an array as follows (not 100% sure if this is the right way to do it - but it works ) mysql_select_db($database_cn, $cn); $query_rsMyQuery = "SELECT columna, columnb, columnc FROM mytable"; $rsMyQuery = mysql_query($query_rsMyQuery, $cn) or die(mysql_error()); $totalRows_rsMyQuery = mysql_num_rows($rsMyQuery); $MyQueryArray = array(); while($row=mysql_fetch_assoc($rsMyQuery)) {array_push($MyQueryArray, $row);} mysql_free_result($rsMyQuery); ======================== Now I want to change a value that is in columnb. For example I have a value called 'placeholder' and I want to replace it with 'hello'. I'm just not sure how to do this. I have tried: str_replace('placeholder','hello',$MyQueryArray); But that didn't do anything Could someone help me with this? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/ Share on other sites More sharing options...
JasonLewis Posted September 15, 2008 Share Posted September 15, 2008 What you would need to do is find the key for that value. $MyQueryArray[array_search("placeholder",$MyQueryArray)] = "hello"; Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-641864 Share on other sites More sharing options...
sweetpea Posted September 15, 2008 Author Share Posted September 15, 2008 Thanks for your reply, I've tried it, but it replaces everything to 'h ' - for all values in all columns, not the result I was looking for. Or am I missing something? Thanks! Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-641870 Share on other sites More sharing options...
JasonLewis Posted September 15, 2008 Share Posted September 15, 2008 Wait... Can you please post the results of this so that we can get a better understanding of your array structure. echo "<pre>"; print_r($MyQueryArray); echo "</pre>"; Just put that after the loop. Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-641873 Share on other sites More sharing options...
sweetpea Posted September 15, 2008 Author Share Posted September 15, 2008 Here's an example of the output, hope this helps. Thanks for your help. <pre>Array ( [0] => Array ( [columna] => placeholder [columnb] => somename1 [columnc] => placeholder ) [1] => Array ( [columna] => valuegoeshere [columnb] => somename2 [columnc] => anothervaluegoeshere ) ) </pre> Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-641879 Share on other sites More sharing options...
Garethp Posted September 15, 2008 Share Posted September 15, 2008 $Find = "placeholder"; $Replace = "hello"; $Array = preg_replace($Find, $Replace, $Array); Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-641883 Share on other sites More sharing options...
sweetpea Posted September 15, 2008 Author Share Posted September 15, 2008 Same result, it's a multidimensional array (?!) Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-641891 Share on other sites More sharing options...
sweetpea Posted September 15, 2008 Author Share Posted September 15, 2008 The solution: unction array_str_replace( $sSearch, $sReplace, &$aSubject ) { foreach( $aSubject as $sKey => $uknValue ) { if( is_array($uknValue) ) { array_str_replace( $sSearch, $sReplace, $aSubject[$sKey] ); } else { $aSubject[$sKey] = str_replace( $sSearch, $sReplace, $uknValue ); } } } array_str_replace( 'test', 'hello', $MyArray ); Thanks to all who responded. Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-641898 Share on other sites More sharing options...
kenrbnsn Posted September 15, 2008 Share Posted September 15, 2008 You can also use the function array_walk_recursive() <?php $test = Array(Array( 'columna' => 'placeholder', 'columnb' => 'somename1', 'columnc' => 'placeholder' ), Array( 'columna' => 'valuegoeshere', 'columnb' => 'somename2', 'columnc' => 'anothervaluegoeshere' ) ); function array_replace(&$item, $key, $rep) { list ($orig,$repl) = explode(',',$rep); $item = ($item == $orig)?$repl:$item; } echo '<pre>Before: ' . print_r($test,true) . '</pre>'; array_walk_recursive($test, 'array_replace', 'placeholder,hello'); echo '<pre>After: ' . print_r($test,true) . '</pre>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-641914 Share on other sites More sharing options...
sweetpea Posted September 15, 2008 Author Share Posted September 15, 2008 Ken, Thank you very much! I'm assuming that it would be faster than the one I had? Just curious, let's say I had two or more values to replace - but don't want to go through the complete array each time like: array_walk_recursive($test, 'array_replace', 'placeholder,hello'); array_walk_recursive($test, 'array_replace', 'placeholder2,hello2'); What would be the best way to tackle that? Thanks! Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-641984 Share on other sites More sharing options...
sasa Posted September 15, 2008 Share Posted September 15, 2008 try <?php function my_replace($search, $replace, $array){ if (is_array($search) and is_array($replace) and count($search) != count($replace)) return false; if (is_array($search)) foreach ($search as $k => $v) $search[$k] = 's:'.strlen($v).':"'.$v.'";'; else $search = 's:'.strlen($search).':"'.$search.'";'; if (is_array($replace)) foreach ($replace as $k => $v) $replace[$k] = 's:'.strlen($v).':"'.$v.'";'; else $replace = 's:'.strlen($replace).':"'.$replace.'";'; $array = serialize($array); $array = str_replace($search, $replace, $array); return unserialize($array); } $test = Array ( Array(array('columna' => 'placeholder', 'columnb' => 'somename1', 'columnc' => 'placeholder'),'columnx' => 'placeholder'), Array('columna' => 'valuegoeshere', 'columnb' => 'somename2', 'columnc' => 'anothervaluegoeshere') ); $test = my_replace(array('somename1', 'somename2'), array('sasa', 'john'), $test); print_r($test); ?> Link to comment https://forums.phpfreaks.com/topic/124299-replace-value-in-an-array/#findComment-642018 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.