[PHP-DEV] Re: Bug #12480 Updated: array_merge_recursive() clobbers existing numeric keys
On Aug 06, Bug Database wrote: ID: 12480 Updated by: andrei Reported By: [EMAIL PROTECTED] Old Status: Open Status: Closed Bug Type: Arrays related Operating System: FreeBSD PHP Version: 4.0.6 New Comment: array_merge* functions are not meant to preserve numeric keys. uhm, forgive me for being stupid, but why not? I was trying to use numeric keys to store the id of db records that didn't start at 0. using array_merge_recursive destroyed this information, so I have to store it in the array itself, which in turn means I can't do stuff like if ($records[$id]['someval']), or ksort on $records, or do foreach ($records as $id = $values) and other nice things like that after I've used array_merge_recursive. and do you think, perhaps, it might be worth noting this in the man pages? regards, peter lowe. Previous Comments: [2001-07-31 05:14:43] [EMAIL PROTECTED] if any of the keys of the first level of arrays are numeric, using array_merge_recursive() will renumber them to start at 0. this destroys keys that are used to record id numbers, etc. [mini:pgl]:~/public_html $ cat test.php #!/usr/local/bin/php -q ? $b[a][4] = 1; $a[3][4] = 2; print_r(array_merge_recursive($a, $b)); ? [mini:pgl]:~/public_html $ ./test.php Array ( [0] = Array ( [4] = 2 ) [a] = Array ( [4] = 1 ) ) ATTENTION! Do NOT reply to this email! To reply, use the web interface found at http://bugs.php.net/?id=12480edit=2 -- This is not the signature you are looking for. Move along. -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] Re: Bug #12480 Updated: array_merge_recursive() clobbers existing numeric keys
On Mon, 06 Aug 2001, Peter Lowe wrote: On Aug 06, Bug Database wrote: ID: 12480 Updated by: andrei Reported By: [EMAIL PROTECTED] Old Status: Open Status: Closed Bug Type: Arrays related Operating System: FreeBSD PHP Version: 4.0.6 New Comment: array_merge* functions are not meant to preserve numeric keys. uhm, forgive me for being stupid, but why not? I was trying to use numeric keys to store the id of db records that didn't start at 0. using array_merge_recursive destroyed this information, so I have to store it in the array itself, which in turn means I can't do stuff like if ($records[$id]['someval']), or ksort on $records, or do foreach ($records as $id = $values) and other nice things like that after I've used array_merge_recursive. and do you think, perhaps, it might be worth noting this in the man pages? Because the idea behind merging two arrays (or at least my idea of it that was the motivation for this function) is to preserve all elements of the input arrays in the output one. If it worked as you suggested and the input arrays had elements with the same numeric keys then only one of those elements would appear in the output array, the rest would be gone. That is how + operator works on array, by the way. So, array_merge_* functions don't take numeric keys into account. -Andrei Politics is for the moment, an equation is for eternity. -- Albert Einstein -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] Re: Bug #12480 Updated: array_merge_recursive() clobbers existing numeric keys
On Aug 06, Andrei Zmievski wrote: On Mon, 06 Aug 2001, Peter Lowe wrote: On Aug 06, Bug Database wrote: ID: 12480 Updated by: andrei Reported By: [EMAIL PROTECTED] Old Status: Open Status: Closed Bug Type: Arrays related Operating System: FreeBSD PHP Version: 4.0.6 New Comment: array_merge* functions are not meant to preserve numeric keys. uhm, forgive me for being stupid, but why not? I was trying to use numeric keys to store the id of db records that didn't start at 0. using array_merge_recursive destroyed this information, so I have to store it in the array itself, which in turn means I can't do stuff like if ($records[$id]['someval']), or ksort on $records, or do foreach ($records as $id = $values) and other nice things like that after I've used array_merge_recursive. and do you think, perhaps, it might be worth noting this in the man pages? Because the idea behind merging two arrays (or at least my idea of it that was the motivation for this function) is to preserve all elements of the input arrays in the output one. If it worked as you suggested and the input arrays had elements with the same numeric keys then only one of those elements would appear in the output array, the rest would be gone. That is how + operator works on array, by the way. So, array_merge_* functions don't take numeric keys into account. -Andrei I agree, you *should* preserve all elements of the input array in the output one, one of those elements being the key. it actually says on the man page: If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended. if the values get appended, why renumber numeric keys from 0? - peter. -- This is not the signature you are looking for. Move along. -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] Re: Bug #12480 Updated: array_merge_recursive() clobbers existing numeric keys
On Mon, 06 Aug 2001, Peter Lowe wrote: I agree, you *should* preserve all elements of the input array in the output one, one of those elements being the key. it actually says on the man page: If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended. if the values get appended, why renumber numeric keys from 0? What would you have them start with? :) Imagine this scenario: $array1 = array('a', 'b', 'c'); $array2 = array(2='d', 3='e', 4='f'); $result = array_merge($array1, $array2); In this case, $result is: Array ( [0] = a [1] = b [2] = c [3] = d [4] = e [5] = f ) What should it look like according to you? -Andrei We all have photographic memories, it's just that some of us don't have any film. -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] Re: Bug #12480 Updated: array_merge_recursive() clobbers existing numeric keys
On Aug 06, Andrei Zmievski wrote: What would you have them start with? :) Imagine this scenario: $array1 = array('a', 'b', 'c'); $array2 = array(2='d', 3='e', 4='f'); $result = array_merge($array1, $array2); In this case, $result is: Array ( [0] = a [1] = b [2] = c [3] = d [4] = e [5] = f ) What should it look like according to you? this: would be: Array ( [0] = a [1] = b [2] = c [3] = e [4] = d [5] = f ) with e and d swapped round. any value in $array2 that already had a key in $array1 would be put on the end of a queue of values to be stuck on the end of $array1 when the other values have finished merging. it's a shame there's no space left on the end of the function that you could put a constant to specify what behaviour you want. -- This is not the signature you are looking for. Move along. -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] Re: Bug #12480 Updated: array_merge_recursive() clobbers existing numeric keys
On Mon, 06 Aug 2001, Peter Lowe wrote: this: would be: Array ( [0] = a [1] = b [2] = c [3] = e [4] = d [5] = f ) with e and d swapped round. any value in $array2 that already had a key in $array1 would be put on the end of a queue of values to be stuck on the end of $array1 when the other values have finished merging. But that's inconsistent with how string-key collision merging works - there, an array containing collision elements is created under the collision key. it's a shame there's no space left on the end of the function that you could put a constant to specify what behaviour you want. yes it is. -Andrei Nobody tried to design Windows - it just grew in random directions without any kind of thought behind it. -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]