0

Angular がデータを簡単にトラバースできるように、mysql 応答をオブジェクトの配列にフォーマットしようとしています。

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 に答える