Array replace recursive()

array_replace_recursive()

1 PHP[ | ]

<?php
$a = array('orange', array(1,2,3), 'banana');
$b = array('pineapple', array(3,4));

print_r( array_replace_recursive($a, $b) );
# Array
# (
#     [0] => pineapple
#     [1] => Array
#         (
#             [0] => 3
#             [1] => 4
#             [2] => 3
#         )
# 
#     [2] => banana
# )

print_r( array_replace_recursive($b, $a) );
# Array
# (
#     [0] => orange
#     [1] => Array
#         (
#             [0] => 1
#             [1] => 2
#             [2] => 3
#         )
# 
#     [2] => banana
# )

2 Ruby[ | ]

def array_replace_recursive(base, replacements)
	base = Marshal.load(Marshal.dump(base))
	replacements.each_with_index do | v, i |
		if( v.kind_of?(Array) )
			base[i] = array_replace_recursive(base[i], v)
		else 
			base[i] = v
		end
	end
	return base
end

a = ["orange", [1,2,3], "banana"]
b = ["pineapple", [3,4]]

puts array_replace_recursive(a, b).inspect
# ["pineapple", [3, 4, 3], "banana"]
puts array_replace_recursive(b, a).inspect
# ["orange", [1, 2, 3], "banana"]

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}