通过查询Google数据存储区返回的数组未正确排序

时间:2018-11-24 23:50:22

标签: php

我使用PHP查询Google云端数据存储区记录。 这是我的PHP代码:

$datastore = new DatastoreClient(['projectId' => $projectId, 'namespaceId' => 'ssd']);
function getList(DatastoreClient $datastore){
    $cursor=null;
    $query = $datastore->query()
        ->kind('keypad_research')
              ->start($cursor);

  $results = $datastore->runQuery($query);
    $entries = [];

    foreach ($results as $entity) {
        $entry = $entity->get();
        $entry['id'] = $entity->key()->pathEndIdentifier();
        $entries[] = $entry;
    }
    return [
        'entries' => $entries,
    ];
}

返回的结果如下:

Array
(
    [entries] => Array
        (
            [0] => Array
                (
                    [phone6] => 1827533132
                    [gender] => Male
                    [phone2] => 1827533132
                    [concept3] => 2.84
                    [concept6] => 0
                    [race] => White
                    [phone4] => 1827533132
                    [age] => 26-35
                    [concept2] => 2.06
                    [concept5] => 3.56
                    [phone3] => 1827533132
                    [phone1] => 1827533132
                    [concept1] => 2.53
                    [phone5] => 1827533132
                    [concept4] => 12.8100
                    [id] => 4795887050031104
                )

            [1] => Array
                (
                    [phone3] => 1234567890
                    [phone1] => 1234567890
                    [concept1] => 4.16
                    [phone5] => 1234567890
                    [concept4] => 2.7300
                    [phone6] => 0
                    [gender] => Male
                    [phone2] => 1234567890
                    [concept3] => 2.71
                    [concept6] => 0
                    [race] => Black
                    [phone4] => 1234567890
                    [age] => 46-65
                    [concept2] => 3.6
                    [concept5] => 3.36
                    [id] => 4796717991985152
                )

            [2] => Array
                (
                    [race] => White
                    [phone4] => -442751970
                    [age] => 18-25
                    [concept2] => 54.84
                    [concept5] => 3.17
                    [phone3] => -442751970
                    [phone1] => -442751970
                    [concept1] => 2.62
                    [phone5] => -442751970
                    [concept4] => 2.3700
                    [phone6] => -442751970
                    [gender] => Male
                    [phone2] => -442751970
                    [concept3] => 3.29
                    [concept6] => 2.58
                    [id] => 4804278879256576
                )
        )
)

如您所见,数组项的排序方式不同,每个数组的排序方式不同。我想知道是否应该以不同的方式编写代码,以使返回的每个数组都具有相同的项顺序?

我希望每个数组项都像这样:

[2] => Array
                (
                    [id] => 4804278879256576
                    [phone1] => -442751970
                    [concept1] => 2.62
                    [phone2] => -442751970
                    [concept2] => 54.84
                    [phone3] => -442751970
                    [concept3] => 3.29
                    [phone4] => -442751970
                    [concept4] => 2.3700
                    [phone5] => -442751970
                    [concept5] => 3.17
                    [phone6] => -442751970
                    [concept6] => 2.58
                    [race] => White
                    [age] => 18-25
                    [gender] => Male
                )

谢谢

0 个答案:

没有答案