Page 1 of 1
Changing PHP array() elements
Posted: Wed Oct 27, 2004 6:08 am
by mjseaden
Hi
If I call substr() on a single variable, let's call it $var, then it seems to work. However, if I call the same substr() on an array element, for example, $array[0], then it doesn't change it. I also can't seem to set array elements with alternative values either.
Do I take it you can't alter elements of arrays in PHP?
Thanks
Mark
Posted: Wed Oct 27, 2004 6:31 am
by CoderGoblin
OK I am confused... substr does not change a variable, just returns what you want substring.
Code: Select all
$var='Hello There';
echo(substr($var,6);
$var=substr($var,0,5);
should set var $to 'Hello' after displaying 'There';
Code: Select all
$array=array('Hello There', 'How you doing');
$array[0]=substr($array[0],0,5);
should change the first element of the array to 'Hello';
Code: Select all
$array=array('Hello There', 'How you doing');
foreach ($array as $key=>$value) {
$array[$key]=strtoupper($value);
}
Converts all of array to uppercase.
If you have a specific example of the problem (php code) it would be useful.
Re: Changing PHP array() elements
Posted: Wed Oct 27, 2004 7:31 am
by patrikG
mjseaden wrote:Hi
If I call substr() on a single variable, let's call it $var, then it seems to work. However, if I call the same substr() on an array element, for example, $array[0], then it doesn't change it. I also can't seem to set array elements with alternative values either.
Do I take it you can't alter elements of arrays in PHP?
Thanks
Mark
subst() is a function for strings. Arrays a different type of variable. See
http://uk.php.net/manual/en/language.types.php