PHP array replace recursive()

Jmnote bot (토론 | 기여)님의 2020년 11월 2일 (월) 02:34 판 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)
PHP array_replace_recursive()

1 예시 1[ | ]

PHP
Copy
$base = array(
	'citrus' => array("orange"),
	'berries' => array("blackberry", "raspberry"),
);
$replacements = array(
	'citrus' => array('pineapple'),
	'berries' => array('blueberry')
);
$basket = array_replace_recursive($base, $replacements);
print_r($basket);
# Array
# (
#     [citrus] => Array
#         (
#             [0] => pineapple
#         )
# 
#     [berries] => Array
#         (
#             [0] => blueberry
#             [1] => raspberry
#         )
# 
# )

2 예시 2[ | ]

PHP
Copy
$base = array(
	'citrus' => array("orange"),
	'berries' => array("blackberry", "raspberry"),
	'others' => 'banana'
);
$replacements = array(
	'citrus' => 'pineapple',
	'berries' => array('blueberry'),
	'others' => array('litchis')
);
$replacements2 = array(
	'citrus' => array('pineapple'),
	'berries' => array('blueberry'),
	'others' => 'litchis'
);
$basket = array_replace_recursive($base, $replacements, $replacements2);
print_r($basket);
# Array
# (
#     [citrus] => Array
#         (
#             [0] => pineapple
#         )
# 
#     [berries] => Array
#         (
#             [0] => blueberry
#             [1] => raspberry
#         )
# 
#     [others] => litchis
# )

3 예시 3[ | ]

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',
		'mouser' => 'LG',
	)
);

$merged1 = array_replace_recursive($computer1, $computer2);
$merged2 = array_replace_recursive($computer2, $computer1);

print_r( $merged1 );
# Array
# (
#     [owner] => Jane
#     [softwares] => Array
#         (
#             [0] => Linux
#             [1] => Apache
#             [2] => MySQL
#             [3] => PHP
#             [4] => Apache
#         )
# 
#     [peripherals] => Array
#         (
#             [keyboard] => LG
#             [mouser] => LG
#         )
# 
# )

print_r( $merged2 );
# Array
# (
#     [owner] => John
#     [softwares] => Array
#         (
#             [0] => Windows
#             [1] => Office
#             [2] => Photoshop
#             [3] => Python
#             [4] => Apache
#         )
# 
#     [peripherals] => Array
#         (
#             [keyboard] => Samsung
#             [mouser] => LG
#         )
# 
# )

4 같이 보기[ | ]

5 참고[ | ]

편집자 J Jmnote Jmnote bot
  • PHP 파일 다운로드 구현 2 (한글 파일명 지원)
    제가 파일다운로드 관련된 것이 서툴러서 파일 다운로드 부분을 인용하였습니다. 죄송합니다.
  • PHP에서 오라클 DB 사용
    감사합니당. YoWu
  • PHP 파일 업로드 구현
    파일정보 출력에서 $name $ext 가 정상적으로 출력되나요? 전부 echo의 쌍따옴표안에 있는데? 일리단사오육칠
  • PHP 파일 업로드 구현
    PHP echo에서 쌍따옴표 안의 변수는 해석되어 출력됩니다. 위 소스에서도 $name와 $ext가 두껍게 나와있죠? 일반 문자열과는 다르다는 표시죠.J Jmnote
  • PHP 파일 다운로드 구현 2 (한글 파일명 지원)
    'Windows NT 6.1' << 이부분은 접속한 윈도우의 플랫폼을 뜻하는걸로 압니다. ie11 접속하면 다음과 같이 바뀌었고 Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko rv:11.0 << 이부분이 Anmkst
  • Lib my.php
    Fatal error: Uncaught Error: Call to undefined function insert_rows() in D:\xampp\htdocs\naru\import_excel.php:38 Stack trace: #0 {main} thrown in D:\xampp\htdo 신정섭
  • Lib my.php
    함수 query()를 사용하면 됩니다. 예시 추가했으니 참고바랍니다.J Jmnote
  • 로또번호 생성
    초보를 위한 개발 실습 과제로군요 ㅎㅎ Pinkcrimson