Jump to content

Recommended Posts

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

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>

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.

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

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!

 

 

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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.