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
Changing PHP array() elements
Moderator: General Moderators
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
OK I am confused... substr does not change a variable, just returns what you want substring.
should set var $to 'Hello' after displaying 'There';
should change the first element of the array to 'Hello';
Converts all of array to uppercase.
If you have a specific example of the problem (php code) it would be useful.
Code: Select all
$var='Hello There';
echo(substr($var,6);
$var=substr($var,0,5);Code: Select all
$array=array('Hello There', 'How you doing');
$array[0]=substr($array[0],0,5);Code: Select all
$array=array('Hello There', 'How you doing');
foreach ($array as $key=>$value) {
$array[$key]=strtoupper($value);
}If you have a specific example of the problem (php code) it would be useful.
Re: Changing PHP array() elements
subst() is a function for strings. Arrays a different type of variable. See http://uk.php.net/manual/en/language.types.phpmjseaden 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