如何在Laravel中将数组对象和数据添加到JSON

时间:2019-03-13 20:54:20

标签: php json laravel

如何在Laravel中将数组对象和数据添加到JSON

$json = {'id':5, 'name':'Hassan'};

我想向role添加新对象Admin和值$json

我想要类似的结果

$json = {'id':5, 'name':'Hassan', 'role':'Admin'};

2 个答案:

答案 0 :(得分:2)

您可以解码JSON,添加所需的值,然后将其编码回JSON:

$data = json_decode($json, true);

$data['role'] = 'Admin';

$json = json_encode($json);

json_decode() Docs
json_encode() Docs

答案 1 :(得分:1)

尝试一下

Sub FindResults()
'Selecting file for import'
        Dim FileSelect As Object
            Dim PlateMapFolder As String
                PlateMapFolder = "C:\"
        Set FileSelect = Application.FileDialog(msoFileDialogFilePicker)

                With FileSelect
                    .InitialFileName = PlateMapFolder
                    .AllowMultiSelect = False
                    .Title = "Please select associated run"
                    .Show

                        If .SelectedItems.Count = 0 Then
                        Exit Sub
                        End If

                    SelectedFile = Dir(.SelectedItems(1))
                End With

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Splitting SelectedFile
Const strSearch = "[Results]"
Dim intFileNumber As Integer

intFileNumber = FreeFile

Open SelectedFile For Input As intFileNumber
    strFileContent = Input(LOF(intFileNumber), intFileNumber)

    'Split result file at [Results]
    strResults = Split(strFileContent, strSearch)

    'Split line breaks
    arrResultsLine = Split(strResults(1), vbLf)

   'Split each line by tab
   intRow = 1
    For i = 2 To UBound(arrResultsLine) - 1
        arrResultsTab = Split(arrResultsLine(i), vbTab)
                Sheets("RawData").Range("A" & CStr(intRow)).Value = arrResultsTab(0)
                Sheets("RawData").Range("B" & CStr(intRow)).Value = arrResultsTab(1)
                Sheets("RawData").Range("C" & CStr(intRow)).Value = arrResultsTab(2)
                Sheets("RawData").Range("D" & CStr(intRow)).Value = arrResultsTab(3)
                Sheets("RawData").Range("E" & CStr(intRow)).Value = arrResultsTab(4)
            intRow = intRow + 1
        Next i

End Sub