Array returned by querying Google datastore is not ordered correctly





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I an using PHP to query Google cloud datastore records.
This is my PHP code:



$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,
];
}


The result returned look like this:



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
)
)
)


As you can see, the array items are not ordered in the same way, each array is ordered differently. I was wondering if I should write the code differently so that every array returned has same order of items?



I would like every array item to be like this:



[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
)


Thanks










share|improve this question























  • Although it's untidy for printing, what difference does it make? you can still access the elements as $array['entries'][$i]['phone2'] regardless of internal ordering in the array.

    – Nick
    Nov 24 '18 at 23:54











  • Thats right, I can still access as you mentioned. I was just wondering that if I could make it cleaner.

    – ssdesign
    Nov 25 '18 at 0:03






  • 1





    You could just use ksort($entry); before $entries = $entry;

    – Nick
    Nov 25 '18 at 0:04











  • Thanks Nick, that does sort it :) Not in the specific order i want, but now they are all same order.

    – ssdesign
    Nov 25 '18 at 0:42











  • You could use uksort to sort in the exact order you want, but it would be a pretty complicated sort function...

    – Nick
    Nov 25 '18 at 0:43


















0















I an using PHP to query Google cloud datastore records.
This is my PHP code:



$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,
];
}


The result returned look like this:



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
)
)
)


As you can see, the array items are not ordered in the same way, each array is ordered differently. I was wondering if I should write the code differently so that every array returned has same order of items?



I would like every array item to be like this:



[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
)


Thanks










share|improve this question























  • Although it's untidy for printing, what difference does it make? you can still access the elements as $array['entries'][$i]['phone2'] regardless of internal ordering in the array.

    – Nick
    Nov 24 '18 at 23:54











  • Thats right, I can still access as you mentioned. I was just wondering that if I could make it cleaner.

    – ssdesign
    Nov 25 '18 at 0:03






  • 1





    You could just use ksort($entry); before $entries = $entry;

    – Nick
    Nov 25 '18 at 0:04











  • Thanks Nick, that does sort it :) Not in the specific order i want, but now they are all same order.

    – ssdesign
    Nov 25 '18 at 0:42











  • You could use uksort to sort in the exact order you want, but it would be a pretty complicated sort function...

    – Nick
    Nov 25 '18 at 0:43














0












0








0








I an using PHP to query Google cloud datastore records.
This is my PHP code:



$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,
];
}


The result returned look like this:



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
)
)
)


As you can see, the array items are not ordered in the same way, each array is ordered differently. I was wondering if I should write the code differently so that every array returned has same order of items?



I would like every array item to be like this:



[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
)


Thanks










share|improve this question














I an using PHP to query Google cloud datastore records.
This is my PHP code:



$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,
];
}


The result returned look like this:



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
)
)
)


As you can see, the array items are not ordered in the same way, each array is ordered differently. I was wondering if I should write the code differently so that every array returned has same order of items?



I would like every array item to be like this:



[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
)


Thanks







php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 23:50









ssdesignssdesign

1,12062341




1,12062341













  • Although it's untidy for printing, what difference does it make? you can still access the elements as $array['entries'][$i]['phone2'] regardless of internal ordering in the array.

    – Nick
    Nov 24 '18 at 23:54











  • Thats right, I can still access as you mentioned. I was just wondering that if I could make it cleaner.

    – ssdesign
    Nov 25 '18 at 0:03






  • 1





    You could just use ksort($entry); before $entries = $entry;

    – Nick
    Nov 25 '18 at 0:04











  • Thanks Nick, that does sort it :) Not in the specific order i want, but now they are all same order.

    – ssdesign
    Nov 25 '18 at 0:42











  • You could use uksort to sort in the exact order you want, but it would be a pretty complicated sort function...

    – Nick
    Nov 25 '18 at 0:43



















  • Although it's untidy for printing, what difference does it make? you can still access the elements as $array['entries'][$i]['phone2'] regardless of internal ordering in the array.

    – Nick
    Nov 24 '18 at 23:54











  • Thats right, I can still access as you mentioned. I was just wondering that if I could make it cleaner.

    – ssdesign
    Nov 25 '18 at 0:03






  • 1





    You could just use ksort($entry); before $entries = $entry;

    – Nick
    Nov 25 '18 at 0:04











  • Thanks Nick, that does sort it :) Not in the specific order i want, but now they are all same order.

    – ssdesign
    Nov 25 '18 at 0:42











  • You could use uksort to sort in the exact order you want, but it would be a pretty complicated sort function...

    – Nick
    Nov 25 '18 at 0:43

















Although it's untidy for printing, what difference does it make? you can still access the elements as $array['entries'][$i]['phone2'] regardless of internal ordering in the array.

– Nick
Nov 24 '18 at 23:54





Although it's untidy for printing, what difference does it make? you can still access the elements as $array['entries'][$i]['phone2'] regardless of internal ordering in the array.

– Nick
Nov 24 '18 at 23:54













Thats right, I can still access as you mentioned. I was just wondering that if I could make it cleaner.

– ssdesign
Nov 25 '18 at 0:03





Thats right, I can still access as you mentioned. I was just wondering that if I could make it cleaner.

– ssdesign
Nov 25 '18 at 0:03




1




1





You could just use ksort($entry); before $entries = $entry;

– Nick
Nov 25 '18 at 0:04





You could just use ksort($entry); before $entries = $entry;

– Nick
Nov 25 '18 at 0:04













Thanks Nick, that does sort it :) Not in the specific order i want, but now they are all same order.

– ssdesign
Nov 25 '18 at 0:42





Thanks Nick, that does sort it :) Not in the specific order i want, but now they are all same order.

– ssdesign
Nov 25 '18 at 0:42













You could use uksort to sort in the exact order you want, but it would be a pretty complicated sort function...

– Nick
Nov 25 '18 at 0:43





You could use uksort to sort in the exact order you want, but it would be a pretty complicated sort function...

– Nick
Nov 25 '18 at 0:43












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463441%2farray-returned-by-querying-google-datastore-is-not-ordered-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463441%2farray-returned-by-querying-google-datastore-is-not-ordered-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings