Page 1 of 1

array search and replace

Posted: Fri Jul 16, 2010 7:07 pm
by pedroz
with the following array

$array = ([1zxc] => 'a/abc/m.htm', [2zac] => 'b/attachment/d.htm', [3c] => 'd/dsds/hre23.htm', [4zac] => 'k/attachment/d.htm');

How can i replace in the array all '/attachment/' for '/attach/' and get...?

$array = ([1zxc] => 'a/abc/m.htm', [2zac] => 'b/attach/d.htm', [3c] => 'd/dsds/hre23.htm', [4zac] => 'k/attach/d.htm');

Re: array search and replace

Posted: Fri Jul 16, 2010 11:01 pm
by Bind
PHP's str_replace() function

str_replace ( $needle , $replacement_needle , $haystack [, count ] )

example:

Code: Select all

<?php
$paths = ([1zxc] => 'a/abc/m.htm', [2zac] => 'b/attachment/d.htm', [3c] => 'd/dsds/hre23.htm', [4zac] => 'k/attachment/d.htm');
$paths = str_replace('attachment','attach',$paths);
?>
NOTE: in your code, $array is an illegal variable name in PHP as it's a reserved word, so when you try it, name your array something other than $array.

Re: array search and replace

Posted: Sat Jul 17, 2010 3:29 am
by pedroz
Many thanks! You are right, should not call it $array.
However, I did a mistake asking for the replacement. I need the following...

$Sarray = ([1zxc] => 'a/abc/m.htm', [2zac] => 'b/attachment/d.htm', [3c] => 'd/dsds/hre23.htm', [4zac] => 'k/attachment/d.htm');

replacing za for TW

$Sarray = ([1zxc] => 'a/abc/m.htm', [2TWc] => 'b/attachment/d.htm', [3c] => 'd/dsds/hre23.htm', [4TWc] => 'k/attachment/d.htm')

Presume I have to unset the 2zac and 4zac... How is the fastest way to do it?
Thanks again...

Re: array search and replace

Posted: Sat Jul 17, 2010 4:09 pm
by buckit
so you need to change the key and not the value?

This should work... but I would recommend using this as just an example to learn how to write a better one :)

Code: Select all

function findReplaceArrayKeyName($hayStack, $find, $replace){
	foreach($hayStack as $k=>$v){
		$k = str_replace($find,$replace,$k);
		$newArray[$k] = $v;
	}
	return $newArray;
}

Re: array search and replace

Posted: Sat Jul 17, 2010 4:21 pm
by AbraCadaver
Another example, only because I don't like loops if I can get away from them (even though that's a standard way of modifying arrays) and I like array functions. :)

Code: Select all

$Sarray = array_combine(str_replace('za', 'TW', array_keys($Sarray)), array_values($Sarray));
Also, in PHP reserved words CAN be used as variables, just NOT constants, class names, function or method names. I use $array in examples, but in production code you should use something descriptive. In this case $paths, $pages, $attachments or whatever makes sense.

Re: array search and replace

Posted: Mon Jul 19, 2010 3:47 am
by pedroz

Code: Select all

$Sarray = array ('/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2] [()/trackback/?$', 'xyz/atttachment/test' => 'index.php?attachment=$matches[1]&cpage=$matches[2] [()/trackback/?$');

$Sarray  = array_combine(str_replace('/attachment/', '/attach/', array_keys($Sarray)), array_values($Sarray));
print_r($Sarray);
why doesn't it works for the second element of the array? it only replace the first one...

Re: array search and replace

Posted: Mon Jul 19, 2010 12:25 pm
by AbraCadaver
My example was to replace the keys. Use Bind's example to replace attachment in the values.