I dare you to improve this

Pre PHP 5.3 array_replace_recursive()

This method does not actually require a recursive call to the function, so it can be inlined in the event you only need it in one place and don't care to populate fake native functions for whatever reason.

array_replace_recursive.php
<?php function array_replace_recursive($base, $replacements) { foreach (array_slice(func_get_args(), 1) as $replacements) { $bref_stack = array(&$base); $head_stack = array($replacements); do { end($bref_stack); $bref = &$bref_stack[key($bref_stack)]; $head = array_pop($head_stack); unset($bref_stack[key($bref_stack)]); foreach (array_keys($head) as $key) { if (isset($bref[$key]) && is_array($bref[$key]) && is_array($head[$key])) { $bref_stack[] = &$bref[$key]; $head_stack[] = $head[$key]; } else { $bref[$key] = $head[$key]; } } } while(count($head_stack)); } return $base; }

Reactions 5 reactions

Poef Auke van Slooten commented on this code
-28
I don't think I can improve the code perse, but for mere mortals a few choice comments would be useful. Its clear you are using an internal stack to avoid recursion - you might mention it. You could also change the name bref_stack, as it stands I don't know what bref means - I guess 'base reference'? I would also explicitly document that you can add a second, third etc. set of replacements to the function.
timw4mail Timothy J. Warren commented on this code
-30
I agree on the comments. I'm a fan of using docblock type comments, as well as general process description comments.
Fake51 Peter Lind commented on this code
16
Is there any specific reason you're avoiding the recursive call? When I try to benchmark the function against one that's recursive, this one comes out 1.5 times slower (5.2.6) or 2 times slower (5.4.3). Apart from that I second the call for code comments - also in the signature so it's obvious the function can take any number of arguments.
Poef Auke van Slooten commented on this code
-31
@Peter Lind: interesting, I'd never have guessed that, PHP really has improved a lot :) But perhaps using the SplStack class for the bref_stack and head_stack would increase the performance of this non-recursive version.
Also, when PHP runs out of stack space it just crashes or - with Xdebug installed - throws a fatal error. Not something you are likely to come across often, but this implementation doesn't have that limitation at least.

Another option which may help and make the code more readable as well is to use the RecursiveArrayIterator class to loop over the array. I'm not sure however if that will keep the references to the array intact so you can replace the values in the original array.
destroyer9 super duper commented on this code
-2
http://jotlt.indiana.edu/comment/view/3084/0/1689
http://canadianfoodstudies.uwaterloo.ca/index.php/cfs/comment/view/25/0/116
http://www.plaio.org/index.php/home/comment/view/7/0/206
https://www.ced.ncsu.edu/meridian/index.php/meridian/comment/view/69/0/4003
http://www.cje-rce.ca/index.php/cje-rce/comment/view/852/1162/368
http://www.ijtmb.org/index.php/ijtmb/comment/view/22/35/280
http://www.chiriottieditori.it/ojs/index.php/ijfs/comment/view/70/0/61
http://revistaseletronicas.pucrs.br/ojs/index.php/belt/comment/view/9792/0/342
http://septentrio.uit.no/index.php/SapEdu/comment/view/3509/0/32
http://periodicos.unitau.br/ojs-2.2/index.php/exatas/comment/view/384/0/24363
http://www.ijede.ca/index.php/jde/comment/view/134/411/125
http://impactum-journals.uc.pt/index.php/arspa/comment/view/1364/0/120164
http://forum.gaming.mbroth.net/profile.php?id=275
http://shop.smartline.club/fluxbb/profile.php?id=15985
http://www.ninjaevolution.net/ben/profile.php?id=84705
http://www.satokoserv.raidghost.com/forum/profile.php?id=29768
http://www.netcreative-reims6.fr/forum/profile.php?id=617118
http://forum.hipointinc.com/profile.php?id=2425
http://ecologicalurbanism.gsd.harvard.edu/forum/profile.php?id=1483888
http://forum.mekong-delta.com/profile.php?id=405270
http://www.gtessentials.org/wiki/TWiki/bin/view.cgi/Main/MeganPhuong
http://wiki.digitalrepositoryauditandcertification.org/bin/view/Main/TimJonas
http://wiki.nurulfikri.ac.id/bin/view/Main/EricPaquin
https://wiki.tiger-computing.co.uk/do/view/Main/JuanMickens
http://wiki.phxx.com/twiki/bin/view/Main/AquilinaValenzuela
http://wiki.nurulfikri.ac.id/bin/view/Main/EricPaquin

Comment on this code:

Please Login or Register to leave a comment.