- associative array replace recursive
- replace_recursive()
1 PHP[ | ]
PHP
Copy
$computer1 = array(
'owner' => 'John',
'softwares' => array( 'Windows', 'Office', 'Photoshop', 'Python', 'Apache' ),
'peripherals' => array( 'keyboard' => 'Samsung' ),
);
$computer2 = array(
'owner' => 'Jane',
'softwares' => array( 'Linux', 'Apache', 'MySQL', 'PHP' ),
'peripherals' => array( 'keyboard' => 'LG', 'mouse' => 'LG' ),
);
print_r( array_replace_recursive($computer1, $computer2) );
# Array
# (
# [owner] => Jane
# [softwares] => Array
# (
# [0] => Linux
# [1] => Apache
# [2] => MySQL
# [3] => PHP
# [4] => Apache
# )
#
# [peripherals] => Array
# (
# [keyboard] => LG
# [mouse] => LG
# )
#
# )
print_r( array_replace_recursive($computer2, $computer1) );
# Array
# (
# [owner] => John
# [softwares] => Array
# (
# [0] => Windows
# [1] => Office
# [2] => Photoshop
# [3] => Python
# [4] => Apache
# )
#
# [peripherals] => Array
# (
# [keyboard] => Samsung
# [mouser] => LG
# )
#
# )
2 Ruby[ | ]
Ruby
Copy
def replace_recursive(base, replacements)
def replace_recursive_real(base, replacements)
if ( replacements.kind_of?(Array) )
replacements.each_with_index do | v, i | base[i] = replace_recursive_real(base[i], v) end
return base
elsif ( replacements.kind_of?(Hash) )
replacements.each do | k, v | base[k] = replace_recursive_real(base[k], v) end
return base
end
replacements
end
replace_recursive_real(Marshal.load(Marshal.dump(base)), replacements)
end
computer1 = {
'owner' => 'John',
'softwares' => [ 'Windows', 'Office', 'Photoshop', 'Python', 'Apache' ],
'peripherals' => { 'keyboard' => 'Samsung' },
'a' => 'hello'
}
computer2 = {
'owner' => 'Jane',
'softwares' => [ 'Linux', 'Apache', 'MySQL', 'PHP' ],
'peripherals' => {'keyboard' => 'LG', 'mouse' => 'LG' },
'b' => 'world'
}
puts replace_recursive(computer1, computer2).inspect
# {"owner"=>"Jane", "softwares"=>["Linux", "Apache", "MySQL", "PHP", "Apache"], "peripherals"=>{"keyboard"=>"LG", "mouse"=>"LG"}, "a"=>"hello", "b"=>"world"}
puts replace_recursive(computer2, computer1).inspect
# {"owner"=>"John", "softwares"=>["Windows", "Office", "Photoshop", "Python", "Apache"], "peripherals"=>{"keyboard"=>"Samsung", "mouse"=>"LG"}, "b"=>"world", "a"=>"hello"}
Ruby
Copy
def replace_recursive(base, replacements)
def replace_recursive_route(base, replacements)
if ( replacements.kind_of?(Array) ) then replace_recursive_array(base, replacements)
elsif ( replacements.kind_of?(Hash) ) then replace_recursive_hash(base, replacements)
else replacements end
end
def replace_recursive_array(base, replacements)
replacements.each_with_index do | v, i | base[i] = replace_recursive_route(base[i], v) end
base
end
def replace_recursive_hash(base, replacements)
replacements.each do | k, v | base[k] = replace_recursive_route(base[k], v) end
base
end
replace_recursive_route(Marshal.load(Marshal.dump(base)), replacements)
end
computer1 = {
'owner' => 'John',
'softwares' => [ 'Windows', 'Office', 'Photoshop', 'Python', 'Apache' ],
'peripherals' => { 'keyboard' => 'Samsung' },
'a' => 'hello'
}
computer2 = {
'owner' => 'Jane',
'softwares' => [ 'Linux', 'Apache', 'MySQL', 'PHP' ],
'peripherals' => {'keyboard' => 'LG', 'mouse' => 'LG' },
'b' => 'world'
}
puts replace_recursive(computer1, computer2).inspect
# {"owner"=>"Jane", "softwares"=>["Linux", "Apache", "MySQL", "PHP", "Apache"], "peripherals"=>{"keyboard"=>"LG", "mouse"=>"LG"}, "a"=>"hello", "b"=>"world"}
puts replace_recursive(computer2, computer1).inspect
# {"owner"=>"John", "softwares"=>["Windows", "Office", "Photoshop", "Python", "Apache"], "peripherals"=>{"keyboard"=>"Samsung", "mouse"=>"LG"}, "b"=>"world", "a"=>"hello"}
3 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.