用另一个替换数组的开头

时间:2016-07-18 14:16:53

标签: php arrays

我需要用第二个数组替换数组的开头,第二个数组替换尽可能多的项目,但保留原始数组的其余部分。

所以,例如:

   $receiver = [1,2,3,4,5];
   $setter = [10,11,12];

会导致[10,11,12,4,5]

以下是目前有效的代码:

// iterate through the values overwriting the receiver
for ($i=0; $i<count($setter); ++$i) {

    // if the stored width is big enough to accept this value
    if ($i < count($receiver)) {

        // copy the value
        $receiver[$i] = $setter[$i];
    }

}

但是有更好的方法吗?

3 个答案:

答案 0 :(得分:2)

这将用于索引数组

$receiver = array_slice($setter,0,count($receiver)) + $receiver;

Live demo

工作原理array_slice会在必要时切断$setter的结尾,以确保其不超过$receiver。然后+运算符将保持左侧的原样,但如果$receiver更长,它会将其额外的元素附加到左侧数组的末尾。

答案 1 :(得分:1)

函数array_splice可以做你想要的事情

or Each HdFt In .Sections.First.Footers
    If HdFt.Exists Then
      If wdDocSrc.Sections.First.Footers(HdFt.Index).Exists Then
        HdFt.Range.FormattedText = wdDocSrc.Sections.First.Footers(HdFt.Index).Range.FormattedText

                'FILE NAME CODE
                'Check if the DocName bookmark exists
                If wdDocTgt.Bookmarks.Exists("DocName") = True Then
                'If DocName bookmark does exist do this
                Set bmRange = wdDocTgt.Bookmarks("DocName").Range
                        'NEW gets the name of the target document and removed the .doc extension
                        oldFilename = wdDocTgt.Name
                        If Right(oldFilename, 5) = ".docx" Then
                        oldFilename = Left(oldFilename, Len(oldFilename) - 5)
                            ElseIf Right(oldFilename, 4) = ".doc" Then
                            oldFilename = Left(oldFilename, Len(oldFilename) - 4)

                'Update bmRange (DocName bookmark) with the file name with no extension
                bmRange.Text = oldFilename
                        End If
                End If

                 If wdDocTgt.Bookmarks.Exists("DocName2") = True Then
                'If DocName bookmark does exist do this

                 Set bmRange = wdDocTgt.Bookmarks("DocName2").Range
                 'set bmRange as blank so as to no duplicate the name
                 bmRange.Text = " "
                    'NEW gets the name of the target document and removed the .doc extension
                        oldFilename = ""
                        oldFilename = wdDocTgt.Name
                        If Right(oldFilename, 5) = ".docx" Then
                        oldFilename = Left(oldFilename, Len(oldFilename) - 5)
                            ElseIf Right(oldFilename, 4) = ".doc" Then
                            oldFilename = Left(oldFilename, Len(oldFilename) - 4)

                'Update bmRange (DocName bookmark) with the file name with no extension
                 bmRange.Text = oldFilename
                 End If
             End If
            'END FILE NAME CODE

      End If
    End If

<强> demo

答案 2 :(得分:1)

以下是您需求的简单代码。

// iterate through the values overwriting the receiver 
for ($i = 0; $i < count($receiver); ++$i) {
    if (isset($setter[$i])) {
        // copy the value
        $receiver[$i] = $setter[$i];
    }
}