Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to format the a mysql response into a array of objects so that Angular can easily traverse the data.

mysql result:

[{
    "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"
}......

here it the 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); 

this is what i get:

{
"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"
..........

desired result:

[
  {
    "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"
..........

I want to know how to have the data structure without the unique keys prefixing the nested arrays

share|improve this question
$json = array('users'=>array(array('name'=>'name one', ...), array('name'=> 'name two'), ...)); – cale_b 16 hours ago
the problem is that each row in the result set writes over the last if I do it that way? – user2762149 15 hours ago

1 Answer

up vote 0 down vote accepted

The thing is that if you define keys, json_encode will read them. If you don't want keys, declare the array without them. So, what you need to do is to structure your array according to you desired output.

But the way your data is arranged make it somewhat hard to format it properly. The solution I can see is to do what you're already doing:

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'];

}

But then you revisit the array and remove all the keys using 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)));

Which gives a result like this:

[
    {
        "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"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
share|improve this answer
doesn't allow for multiple users? – user2762149 14 hours ago
What do you mean by "multiple users"? This way it'll have a "users" key which value is an array of users, like the example you gave. – George Marques 14 hours ago
i already have a complete working output with unique keys (it is in the original post). I want to know how to have the data structure without the unique keys prefixing the nested arays – user2762149 14 hours ago
@user2762149 I edited the answer, see if it is what you want (also, a sample of your desired output would be good, I don't know why you removed from the question). – George Marques 14 hours ago
i felt like it was confusing the question. the format i have in the question is what I want EXCEPT i don't what the indexes prefixing the nested arrays. the code you posted is the same as before? – user2762149 14 hours ago
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.