PHP에서 슬라이스는 배열이나 슬라이스에서 요소의 일부를 선택할 수 있게 해주는 일반적으로 사용되는 데이터 구조입니다. 그러나 때로는 슬라이스를 수정하고 수정된 값을 원래 슬라이스에 추가해야 하는 경우도 있습니다. 이 작업은 PHP에서 슬라이스 복사본의 값을 수정한 다음 array_splice 함수를 사용하여 수정된 복사본을 원래 슬라이스에 추가함으로써 가능합니다. 이 기사에서는 이 방법을 사용하여 슬라이스 수정 및 추가 작업을 구현하는 방법을 자세히 설명합니다.
질문 내용
슬라이스를 복사하고 슬라이스 복사본에 있는 항목의 값을 수정한 다음 복사본을 원본 복사본에 추가하는 함수를 작성하세요. 코드가 슬라이스 복사본뿐만 아니라 원본 슬라이스도 수정하는 것처럼 보입니다.
놀이터로 가기
으아악내가 실제로 본 것
으아악무엇을 기대하나요
import "fmt" type item struct { name string number int } func main() { names := []string{"a", "b"} numbers := []int{1, 2} items := []*item{} for _, name := range names { item := &item{name: name} items = append(items, item) } for n, i := range numbers { if n > 0 { fmt.println(n, "make a copy of the items") itemcopies := make([]*item, len(items)) copy(itemcopies, items) fmt.println(n, "set the numbers on the copies") for _, item := range itemcopies { item.number = i } fmt.println(n, "append the copies to the original") items = append(items, itemcopies...) } else { fmt.println(n, "first pass set all the items to the first number") for _, item := range items { item.number = i } } } for n, i := range items { fmt.printf("%d %+v\n", n, *i) } }
Solution
@jimb 맞습니다. 포인터를 복사하세요.
새 복사본을 생성하려면 구조체 항목에 속하는 복사 함수를 정의하는 것이 좋습니다
위 내용은 슬라이스 복사본의 값을 수정하고 원본 슬라이스에 추가의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

goisidealforbuildingscalablesystemsduetoitssimplicity, 효율성 및 빌드-내부 컨 컨 오렌 스upport.1) go'scleansyntaxandminimalisticdesignenenhance-reductivityandreduceerrors.2) itsgoroutinesandChannelsableefficedsoncurrentProgramming, DistributingLoa

initTectionsIntOnaUtomaticallyBeforemain () andAreSefulforsettingupenvirondentAnitializingVariables.usethemforsimpletasks, propoysideeffects 및 withtestingntestingandloggingtomaincodeclarityAndestability.

goinitializespackages는 theyareimported, theexecutesinitfunctions, theneiredefinitionorder, andfilenamesDeterMineDeTerMineTeRacrossMultipleFiles.ThemayLeadTocomplexInitializations의 의존성 의존성의 의존성을 확인합니다

CustomInterfacesingoAreCrucialForwritingFlectible, 관리 가능 및 TestAblEcode.theyenabledeveloperstofocusonBehaviorimplementation, 향상 ModularityAndRobustness

시뮬레이션 및 테스트에 인터페이스를 사용하는 이유는 인터페이스가 구현을 지정하지 않고 계약의 정의를 허용하여 테스트를보다 고립되고 유지 관리하기 쉽기 때문입니다. 1) 인터페이스를 암시 적으로 구현하면 테스트에서 실제 구현을 대체 할 수있는 모의 개체를 간단하게 만들 수 있습니다. 2) 인터페이스를 사용하면 단위 테스트에서 서비스의 실제 구현을 쉽게 대체하여 테스트 복잡성과 시간을 줄일 수 있습니다. 3) 인터페이스가 제공하는 유연성은 다른 테스트 사례에 대한 시뮬레이션 동작의 변화를 허용합니다. 4) 인터페이스는 처음부터 테스트 가능한 코드를 설계하여 코드의 모듈성과 유지 관리를 향상시키는 데 도움이됩니다.

GO에서는 INT 기능이 패키지 초기화에 사용됩니다. 1) INT 기능은 패키지 초기화시 자동으로 호출되며 글로벌 변수 초기화, 연결 설정 및 구성 파일로드에 적합합니다. 2) 파일 순서로 실행할 수있는 여러 개의 초기 함수가있을 수 있습니다. 3)이를 사용할 때 실행 순서, 테스트 난이도 및 성능 영향을 고려해야합니다. 4) 부작용을 줄이고, 종속성 주입을 사용하고, 초기화를 지연하여 초기 기능의 사용을 최적화하는 것이 좋습니다.

go'selectStatementsTreamLinesconcurramprogrammingBymultiplexingOperations.1) ItallowSwaitingOnMultipLechannelOperations, executingThefirStreadYone.2) thedefaultCasePreventsDeadLocksHavingThepRamToproCeedifNooperationSready.3) Itcanusedfored

Contextandwaitgroupsarecrucialingformaninggoroutineeseforoutineeseferfectial


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

WebStorm Mac 버전
유용한 JavaScript 개발 도구
