Re: making changes on arrays
| From: | Curt Zirzow | Date: | Fri, 30 Apr 2004 05:08:37 +0000 |
| Subject: | Re: making changes on arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185008@lists.php.net to get a copy of this message | ||
* Thus wrote Katie Marquez (katie_marquez2004@yahoo.com):
> I want to have an array, perform an action on the
> values in the array, and have those actions
> permanently affect the values in the array. Here's
> something I've written to test it out how I thought it
> could be done:
>
> <?php
> $array = ('zero\0', 'one\1', 'two\2', 'three\3');
> // look at array before changes
> print_r($array);
> // maybe use foreach or array_walk next?
> foreach ($array as $value)
> {
> stripslashes($value);
First: Most php functions always return a modified version of the
variable instead of modifying them directly. So in order to
actually keep the changes:
$value = stripslashes($value);
Second:
If you are using php 5 you can do something like, note the way
$value is referenced with &:
foreach($array as &$value) {
$value = stripslashes($value);
}
php 4's foreach always works on a copy so you have to access the
array directly:
foreach($array as $index => $value) {
$array[$index] = $value;
}
Curt
--
"I used to think I was indecisive, but now I'm not so sure."