0

我正在尝试将 mysql 响应格式化为对象数组,以便 Angular 可以轻松遍历数据。

mysql结果:

[{
    "FIAccountsID": "99",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "1"
},
{
    "FIAccountsID": "99",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "2"
},
{
    "FIAccountsID": "100",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "1"
},
{
    "FIAccountsID": "100",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "2"
},
{
    "FIAccountsID": "101",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "1"
},
{
    "FIAccountsID": "101",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "2"
},
{
    "FIAccountsID": "102",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "1"
},
{
    "FIAccountsID": "102",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "2"
},
{
    "FIAccountsID": "103",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "1"
},
{
    "FIAccountsID": "103",
    "AccountName": "",
    "AccountCustomName": "Brothers",
    "AccountNumber": "99-123123123",
    "AccountTypeName": "IRA",
    "FinancialInstName": "Testes",
    "FinancialInstID": "9",
    "UserID": "1",
    "ofxStatusCode": "500",
    "UserAccountID": "09128-vc-12",
    "FirstName": "Someones",
    "LastName": "Name",
    "Status": "2"
}......

这里是PHP:

while($row = mysqli_fetch_assoc($result))   
{ 

  $uid = $row['UserID'];
  $name = $row['FirstName'].' '.$row['LastName'];
  $fiid = $row['FinancialInstID'];
  $fi = $row['FinancialInstName'];
  $acctID = $row['FIAccountsID'];


  $rows[$uid]['name'] = $name;
  $rows[$uid]['uid'] = $uid;
  $rows[$uid]['fi'][$fiid]['name'] = $fi;
  $rows[$uid]['fi'][$fiid]['acct'][$acctID]['name'] = $row['AccountCustomName'];

}

print json_encode($rows); 

这就是我得到的:

{
"1": {
    "name": "Some Name",
    "uid": "1",
    "fi": {
        "9": {
            "name": "Testes",
            "accts": {
                "99": {
                    "name": "name 1"
                },
                "100": {
                    "name": "name 2"
                },
                "103": {
                    "name": "name 3"
                }
            }
        }
    }
},
"2": {
    "name": "Another Name",
    "uid": "2",
    "fi": {
        "7": {
            "name": "Trevor's Brokerage House",
            "accts": {
                "1": {
                    "name": "Sally's 401k Account"
                },
                "2": {
                    "name": "retirement"
                },
                "3": {
                    "name": "Some other account"
..........

期望的结果:

[
  {
    "users": [
        {
            "name": "Some Name",
            "uid": "1",
            "fi": [
                {
                    "name": "Testes",
                    "acct": [
                        {
                            "name": "name 1"
                        },
                        {
                            "name": "name 2"
                        },
                        {
                            "name": "name 3"
                        }
                    ]
                }
            ]
        },
        {
            "name": "Another Name",
            "uid": "2",
            "fi": [
                {
                    "name": "Trevor's Brokerage House",
                    "acct": [
                        {
                            "name": "Sally's 401k Account"
                        },
                        {
                            "name": "retirement"
                        },
                        {
                            "name": "Some other account"
..........

我想知道如何在没有唯一键前缀的嵌套数组的情况下拥有数据结构

4

1 回答 1

0

问题是,如果您定义键,json_encode将读取它们。如果您不想要键,请声明没有它们的数组。所以,你需要做的是根据你想要的输出来构造你的数组。

但是数据的排列方式使得正确格式化它有点困难。我能看到的解决方案是做你已经在做的事情:

while($row = mysqli_fetch_assoc($result))   
{ 

  $uid = $row['UserID'];
  $name = $row['FirstName'].' '.$row['LastName'];
  $fiid = $row['FinancialInstID'];
  $fi = $row['FinancialInstName'];
  $acctID = $row['FIAccountsID'];


  $rows[$uid]['name'] = $name;
  $rows[$uid]['uid'] = $uid;
  $rows[$uid]['fi'][$fiid]['name'] = $fi;
  $rows[$uid]['fi'][$fiid]['acct'][$acctID]['name'] = $row['AccountCustomName'];

}

但是然后您重新访问数组并使用以下命令删除所有键array_values

foreach ($rows as $uid => $data)
{
    foreach ($data['fi'] as $fiid => $fi)
    {
        $rows[$uid]['fi'][$fiid]['acct'] = array_values($fi['acct']);
    }
    $rows[$uid]['fi'] = array_values($rows[$uid]['fi']);
}
// Here you format the outer array:
$rows = array(array('users' => array_values($rows)));

这给出了这样的结果:

[
    {
        "users": [
            {
                "name": "Someones Name",
                "uid": "1",
                "fi": [
                    {
                        "name": "Testes",
                        "acct": [
                            {
                                "name": "Brothers"
                            },
                            {
                                "name": "Brothers"
                            },
                            {
                                "name": "Brothers"
                            },
                            {
                                "name": "Brothers"
                            },
                            {
                                "name": "Brothers"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "Someones Name",
                "uid": "2",
                "fi": [
                    {
                        "name": "Testes",
                        "acct": [
                            {
                                "name": "Brothers"
                            },
                            {
                                "name": "Brothers"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
于 2013-10-04T03:29:40.157 回答