Hi guys.

I have a general question about php's built-in array functions.

There is something I have noticed (correct me if I am wrong).

Is it true that (as a general rule) more likely than not, that php's array functions work mainly on the reference rather than a copy of the array?

Its just that there are so many array functions out there and I just need a general rule I can work with.

Can someone shed some light on this?

ps - and can I ask the same question above in regards to php's built in string functions?

Thanks.

Paul.

    I don't think there's an overarching rule, it depends on what it's going to do as to whether it makes sense to use a reference or a copy. Just a quick survey of two functions, and the first uses copies of the arrays, while the second modifies via a reference:

    From www.php.net/array_merge

    array array_merge ( array $array1 [, array $... ] )

    From www.php.net/array_walk_recursive

    bool array_walk_recursive ( array [B][COLOR="#B22222"]&[/COLOR][/B]$array , callable $callback [, mixed $userdata = NULL ] )
                                      ^
                                      |
    

    So the gist of that is you can always check the documentation when you need to know (and if your editor is really smart, it may show you that, as well).

      6 days later

      The two that always used to confuse me was array_walk() and array_map(). They both essentially do the same thing (iterate over the array and apply a callback function to each element), but the former acts on the array itself whereas the latter returns a new array with each element "processed" by the callback. Like NodDog said, there's no hard rule, though I think the majority of the array_* functions end up returning a new array, but some also return other values (like array_sum(), array_rand(), array_reduce(), array_key_exists(), etc.).

        Write a Reply...