Use a json decode array without foreach
I have a following json:
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
Are names and Birthdays. Is there any way to use a criteria to search birthdays without foreach?
For example, "all registry with birthdays after 1993-01-01".
Thanks
php json date foreach
|
show 6 more comments
I have a following json:
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
Are names and Birthdays. Is there any way to use a criteria to search birthdays without foreach?
For example, "all registry with birthdays after 1993-01-01".
Thanks
php json date foreach
1
Any reason why you can't use a foreach loop?
– John Conde
Dec 27 '18 at 16:06
Yes, for example usingarray_filter().
– jeroen
Dec 27 '18 at 16:10
@JohnConde None. But I think that maybe there is a more efficient method, that I return the data according to some criterion, without my having to go through them all ...
– Pedro Antônio
Dec 27 '18 at 16:11
3
I have a working example using array_filter() but I don't want to post it without seeing some sort of effort on your part first
– John Conde
Dec 27 '18 at 16:12
What makes you think a foreach loop is slow or slower than other methods?
– John Conde
Dec 27 '18 at 16:13
|
show 6 more comments
I have a following json:
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
Are names and Birthdays. Is there any way to use a criteria to search birthdays without foreach?
For example, "all registry with birthdays after 1993-01-01".
Thanks
php json date foreach
I have a following json:
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
Are names and Birthdays. Is there any way to use a criteria to search birthdays without foreach?
For example, "all registry with birthdays after 1993-01-01".
Thanks
php json date foreach
php json date foreach
asked Dec 27 '18 at 16:06
Pedro Antônio
15514
15514
1
Any reason why you can't use a foreach loop?
– John Conde
Dec 27 '18 at 16:06
Yes, for example usingarray_filter().
– jeroen
Dec 27 '18 at 16:10
@JohnConde None. But I think that maybe there is a more efficient method, that I return the data according to some criterion, without my having to go through them all ...
– Pedro Antônio
Dec 27 '18 at 16:11
3
I have a working example using array_filter() but I don't want to post it without seeing some sort of effort on your part first
– John Conde
Dec 27 '18 at 16:12
What makes you think a foreach loop is slow or slower than other methods?
– John Conde
Dec 27 '18 at 16:13
|
show 6 more comments
1
Any reason why you can't use a foreach loop?
– John Conde
Dec 27 '18 at 16:06
Yes, for example usingarray_filter().
– jeroen
Dec 27 '18 at 16:10
@JohnConde None. But I think that maybe there is a more efficient method, that I return the data according to some criterion, without my having to go through them all ...
– Pedro Antônio
Dec 27 '18 at 16:11
3
I have a working example using array_filter() but I don't want to post it without seeing some sort of effort on your part first
– John Conde
Dec 27 '18 at 16:12
What makes you think a foreach loop is slow or slower than other methods?
– John Conde
Dec 27 '18 at 16:13
1
1
Any reason why you can't use a foreach loop?
– John Conde
Dec 27 '18 at 16:06
Any reason why you can't use a foreach loop?
– John Conde
Dec 27 '18 at 16:06
Yes, for example using
array_filter().– jeroen
Dec 27 '18 at 16:10
Yes, for example using
array_filter().– jeroen
Dec 27 '18 at 16:10
@JohnConde None. But I think that maybe there is a more efficient method, that I return the data according to some criterion, without my having to go through them all ...
– Pedro Antônio
Dec 27 '18 at 16:11
@JohnConde None. But I think that maybe there is a more efficient method, that I return the data according to some criterion, without my having to go through them all ...
– Pedro Antônio
Dec 27 '18 at 16:11
3
3
I have a working example using array_filter() but I don't want to post it without seeing some sort of effort on your part first
– John Conde
Dec 27 '18 at 16:12
I have a working example using array_filter() but I don't want to post it without seeing some sort of effort on your part first
– John Conde
Dec 27 '18 at 16:12
What makes you think a foreach loop is slow or slower than other methods?
– John Conde
Dec 27 '18 at 16:13
What makes you think a foreach loop is slow or slower than other methods?
– John Conde
Dec 27 '18 at 16:13
|
show 6 more comments
1 Answer
1
active
oldest
votes
I was able to do this with array_filter function
function greater_than($arr){
$date = $arr['bday'];
if(strtotime($date) > strtotime('1993-01-01')){
return $date;
}
}
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
$dec = json_decode($json,true);
print_r(array_filter($dec, "greater_than"));
But is this actually better?
– John Conde
Dec 27 '18 at 16:31
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947783%2fuse-a-json-decode-array-without-foreach%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I was able to do this with array_filter function
function greater_than($arr){
$date = $arr['bday'];
if(strtotime($date) > strtotime('1993-01-01')){
return $date;
}
}
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
$dec = json_decode($json,true);
print_r(array_filter($dec, "greater_than"));
But is this actually better?
– John Conde
Dec 27 '18 at 16:31
add a comment |
I was able to do this with array_filter function
function greater_than($arr){
$date = $arr['bday'];
if(strtotime($date) > strtotime('1993-01-01')){
return $date;
}
}
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
$dec = json_decode($json,true);
print_r(array_filter($dec, "greater_than"));
But is this actually better?
– John Conde
Dec 27 '18 at 16:31
add a comment |
I was able to do this with array_filter function
function greater_than($arr){
$date = $arr['bday'];
if(strtotime($date) > strtotime('1993-01-01')){
return $date;
}
}
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
$dec = json_decode($json,true);
print_r(array_filter($dec, "greater_than"));
I was able to do this with array_filter function
function greater_than($arr){
$date = $arr['bday'];
if(strtotime($date) > strtotime('1993-01-01')){
return $date;
}
}
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
$dec = json_decode($json,true);
print_r(array_filter($dec, "greater_than"));
answered Dec 27 '18 at 16:30
Pedro Antônio
15514
15514
But is this actually better?
– John Conde
Dec 27 '18 at 16:31
add a comment |
But is this actually better?
– John Conde
Dec 27 '18 at 16:31
But is this actually better?
– John Conde
Dec 27 '18 at 16:31
But is this actually better?
– John Conde
Dec 27 '18 at 16:31
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947783%2fuse-a-json-decode-array-without-foreach%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Any reason why you can't use a foreach loop?
– John Conde
Dec 27 '18 at 16:06
Yes, for example using
array_filter().– jeroen
Dec 27 '18 at 16:10
@JohnConde None. But I think that maybe there is a more efficient method, that I return the data according to some criterion, without my having to go through them all ...
– Pedro Antônio
Dec 27 '18 at 16:11
3
I have a working example using array_filter() but I don't want to post it without seeing some sort of effort on your part first
– John Conde
Dec 27 '18 at 16:12
What makes you think a foreach loop is slow or slower than other methods?
– John Conde
Dec 27 '18 at 16:13