将对象的数组转换为数组

时间:2016-04-19 14:16:48

标签: javascript php arrays

将对象的数组转换为数组。

我在JavaScript中创建的数组基于 array.push 函数。我在一个对象中有一个数组,然后我使用 JSON.stringify(myarray)将其转换为数组:

[PensionLimit] =>
    [
        {"member":1,"pension_name":"1A","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
        {"member":1,"pension_name":"1B","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
        {"member":1,"pension_name":"1C","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
        {"member":2,"pension_name":"2A","min":"N/A","max":"N/A","actual":1,"pension_type":"4","result":"N/A"},
        {"member":2,"pension_name":"2B","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
        {"member":2,"pension_name":"2C","min":"N/A","max":"N/A","actual":2000,"pension_type":"4","result":"N/A"},
        {"member":3,"pension_name":"3A","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
        {"member":4,"pension_name":"4A","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"}
    ]

如何转换?

我的预期输出是:

[PensionLimit] => Array
    (
        [1] => Array
            (
                [member] => 1
                [pension_name] => "1A"
                [min] => "N/A"
                [max] => "N/A"
                [actual] => 0
                [pension_type] => "4"
                [result] => "N/A"
            )
        [2] => Array
            (
                [member] => 1
                [pension_name] => "1B"
                [min] => "N/A"
                [max] => "N/A"
                [actual] => 0
                [pension_type] => "4"
                [result] => "N/A"
            )
        [3] => Array
            (
                [member] => 1
                [pension_name] => "1C"
                [min] => "N/A"
                [max] => "N/A"
                [actual] => 0
                [pension_type] => "4"
                [result] => "N/A"
            )
        [4] => Array
            (
                [member] => 1
                [pension_name] => "2A"
                [min] => "N/A"
                [max] => "N/A"
                [actual] => 0
                [pension_type] => "4"
                [result] => "N/A"
            )
        [5] => Array
            (
                [member] => 1
                [pension_name] => "2B"
                [min] => "N/A"
                [max] => "N/A"
                [actual] => 0
                [pension_type] => "4"
                [result] => "N/A"
            )
        [6] => Array
            (
                [member] => 1
                [pension_name] => "3A"
                [min] => "N/A"
                [max] => "N/A"
                [actual] => 0
                [pension_type] => "4"
                [result] => "N/A"
            )
    )

2 个答案:

答案 0 :(得分:1)

我猜您正在尝试将JSON转换为PHP数组,因为这不是有效的JavaScript输出。

为了做到这一点,PHP提供了一个名为json_decode的函数:

'This is done to show automatic generation of a list of reports.
'You might get this from a folder of reports, or a database or similar store
'By adding and taking away from this list you will notice that the code still functions the same.
'Just remember, you would dynamically fill the reports list in your real-world application.
Dim Reports As New List(Of String) From {"Report1", "Report2", "Report3", "Report4", "Report5", "Report6", "Report7", "Report8"}

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'I'm using a flowlayoutpanel so that the controls are added
    'without having to worry about placement
    FlowLayoutPanel1.AutoScroll = True
    'Here is where we loop through the list of reports and add them to the 
    'flowlayoutpanel and give them a common handler
    For Each rpt As String In Reports
        Dim chkRpt As New CheckBox
        chkRpt.Text = rpt
        chkRpt.Height = 17
        chkRpt.Checked = True
        AddHandler chkRpt.MouseDown, AddressOf CustomMouseDown
        FlowLayoutPanel1.Controls.Add(chkRpt)
    Next
End Sub

'Here is the code to allow a right click to select the current checkbox
'and remove all other checked items.
Private Sub CustomMouseDown(sender As Object, e As MouseEventArgs)
    If e.Button = Windows.Forms.MouseButtons.Right Then
        Dim curChk As CheckBox = CType(sender, CheckBox)
        For Each chk As CheckBox In FlowLayoutPanel1.Controls.OfType(Of CheckBox)()
            chk.Checked = False
        Next
        curChk.Checked = True
    End If
End Sub

json_decode($json, true); 结果时,您几乎可以获得预期的输出结果。

答案 1 :(得分:0)

尝试使用.each()循环:

myNewArray = [];
indexOfNestedArray = 0;

PensionLimit.each(function(index1, mySingleObject) {
    mySingleObject.each(function(index2, mySingleObjectSingleValue) {
        myNewArray[index1][index2] = mySingleObjectSingleValue;
    });
    indexOfNestedArray++;
});