Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

str_replace doesn't work when subject is array of arrays

Status
Not open for further replies.

Spork52

Programmer
Nov 20, 2007
134
US
Is str_replace supposed to work when the subject (haystack) is an array of arrays, not just a simple array?

Here's an example:

PHP:
$old_array[0] = array("needle1","needle2","needle3");
$old_array[1] = array("needle4","needle5","needle6");
$new_array = str_replace("needle","pin", $old_array);
echo $new_array[0][0]."<br>";
echo $new_array[0][1]."<br>";
echo $new_array[0][2]."<br>";
echo $new_array[1][0]."<br>";
echo $new_array[1][1]."<br>";
echo $new_array[1][2]."<br>";

The above outputs:

needle1
needle2
needle3
needle4
needle5
needle6

I.e., no replacements are made.

Whereas:

PHP:
$old_array = array("needle1","needle2","needle3");
$new_array = str_replace("needle","pin", $old_array);
echo $new_array[0]."<br>";
echo $new_array[1]."<br>";
echo $new_array[2]."<br>";

outputs:

pin1
pin2
pin3

Am I doing something wrong? Is there an alternative to str_replace that will do what I want?
 
str_replace works only on the first dimension of arrays. And since your first dimension are just other arrays, it cannot actually replace them.

However you can use a for loop or foreach for ease of use, and loop str_replace inside the arrays.


Code:
$old_array[0] = array("needle1","needle2","needle3");
$old_array[1] = array("needle4","needle5","needle6");

foreach($old_array as $key =>[b] $sub_array[/b])
{
[indent]$new_array[] = str_replace("needle","pin", [b]$sub_array)[/b];[/indent]

} 

echo "<pre>" . print_r($new_array,1) . "</pre>";

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thank you. Here's the actual code I used:

PHP:
$circuit_array[0] = array("ch1kwh","ch2kwh","aux1kwh","aux2kwh","aux3kwh","aux4kwh","aux5kwh");
$circuit_array[1] = array("ch1kwh","ch2kwh","aux1kwh","aux2kwh","aux3kwh","aux4kwh","aux5kwh");

foreach ($circuit_array as $value)
	{
	$circuit_alias_field_array[]  = str_replace("kwh","alias", $value);
	$circuit_name_array[] = str_replace(array("kwh","ch","aux"), array("","Ch ","Aux "), $value);
	}

Why isn't the $key required in the square braces?

I also tried:

PHP:
foreach ($circuit_array as $key => $value)
	{
	$circuit_alias_field_array[]  = str_replace("kwh","alias", $value);
	$circuit_name_array[$key] = str_replace("kwh", "", $value);
	$circuit_name_array[$key] = str_replace("ch","Ch ", $circuit_name_array[$key]);
	$circuit_name_array[$key] = str_replace("aux","Aux ", $circuit_name_array[$key]);
	}

which works, but requires the $key.
 
Why isn't the $key required in the square braces?

Because PHP automatically assigns the result to the next available index in the array.

If you need to specify an index (position) within the array, then you need to pass the $key to it. Otherwise, it just gets added to the array at the next numbered index.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top