"Array replace recursive()"의 두 판 사이의 차이

(새 문서: category: Array ;array_replace_recursive() ==PHP== category: PHP <source lang='php'> <?php $computer1 = array( 'owner' => 'John', 'softwares' => array( 'Windows', 'Office',...)
 
잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 12개는 보이지 않습니다)
3번째 줄: 3번째 줄:


==PHP==
==PHP==
{{참고|PHP array_replace_recursive()}}
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
<?php
<?php
$computer1 = array(
$a = array('orange', array(1,2,3), 'banana');
'owner' => 'John',
$b = array('pineapple', array(3,4));
'softwares' => array( 'Windows', 'Office', 'Photoshop', 'Python', 'Apache' ),
'peripherals' => array(
'keyboard' => 'Samsung',
)
);


$computer2 = array(
print_r( array_replace_recursive($a, $b) );
'owner' => 'Jane',
'softwares' => array( 'Linux', 'Apache', 'MySQL', 'PHP' ),
'peripherals' => array(
'keyboard' => 'LG',
'mouser' => 'LG',
)
);
 
$merged1 = array_replace_recursive($computer1, $computer2);
$merged2 = array_replace_recursive($computer2, $computer1);
 
print_r( $merged1 );
# Array
# Array
# (
# (
#    [owner] => Jane
#    [0] => pineapple
#    [softwares] => Array
#    [1] => Array
#        (
#            [0] => Linux
#            [1] => Apache
#            [2] => MySQL
#            [3] => PHP
#            [4] => Apache
#        )
#
#    [peripherals] => Array
#        (
#        (
#            [keyboard] => LG
#            [0] => 3
#            [mouser] => LG
#            [1] => 4
#            [2] => 3
#        )
#        )
#  
#  
#    [2] => banana
# )
# )


print_r( $merged2 );
print_r( array_replace_recursive($b, $a) );
# Array
# Array
# (
# (
#    [owner] => John
#    [0] => orange
#    [softwares] => Array
#    [1] => Array
#        (
#        (
#            [0] => Windows
#            [0] => 1
#            [1] => Office
#            [1] => 2
#            [2] => Photoshop
#            [2] => 3
#            [3] => Python
#            [4] => Apache
#        )
#
#    [peripherals] => Array
#        (
#            [keyboard] => Samsung
#            [mouser] => LG
#        )
#        )
#  
#  
#    [2] => banana
# )
# )
</source>
</syntaxhighlight>
 
==Ruby==
[[category: Ruby]]
<syntaxhighlight lang='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"]
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[replace_recursive()]]
*[[array_merge_recursive()]]
*[[array_merge_recursive()]]
*[[array_replace()]]

2020년 11월 2일 (월) 02:34 기준 최신판

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 }}