Check if key/value pairs exist within array












0















I need to check if a set of query params exists within a larger set of query params. For example, I want to check if array('option' => 'com_pages', 'view' => 'page'), exists within:



Array
(
[option] => com_pages
[format] => html
[view] => page
)


or



Array
(
[option] => com_archive
)


I tried to find a php function that would provide this kind of search, but could't find any that matched what I was looking for. So I created the below function:



function checkRoute($params, $request) {
foreach($params as $key => $value) {
if(!array_key_exists($key, $request)) {
return false;
}

if($request[$key] != $value) {
return false;
}
}

return true;
}


Where $request is an array of the current request's query params.



Is there a quicker way to do this with array functions that I'm missing?










share|improve this question























  • Would you expect it to return true on both of your examples? You'd like it to return true on the empty array too?

    – EarthDragon
    Jul 9 '17 at 5:33






  • 1





    isset( $request[$key] ) will be faster then the array_key_exists(), or at least it was a few years ago ... lol ... semantically I would combing the if's as well, if( !isset( $request[$key] ) || $request[$key] !== $value ) and use strict typing for things like false vs 0 if you care about those edge cases...

    – ArtisticPhoenix
    Jul 9 '17 at 5:35













  • You should also check array_intersect it gives you the intersection of your arrays

    – EarthDragon
    Jul 9 '17 at 5:38






  • 2





    @EarthDragon - that's a good idea but I would use array_intersect_assoc instead so the keys are checked.

    – ArtisticPhoenix
    Jul 9 '17 at 5:39











  • @EarthDragon, No, I'd want false on the second example. I looked at array_intersect_assoc, but I want to check that all the key/value pairs exist in both and that just leaves me with having to check the result, unless I'm missing something.

    – NicholasJohn16
    Jul 9 '17 at 5:42


















0















I need to check if a set of query params exists within a larger set of query params. For example, I want to check if array('option' => 'com_pages', 'view' => 'page'), exists within:



Array
(
[option] => com_pages
[format] => html
[view] => page
)


or



Array
(
[option] => com_archive
)


I tried to find a php function that would provide this kind of search, but could't find any that matched what I was looking for. So I created the below function:



function checkRoute($params, $request) {
foreach($params as $key => $value) {
if(!array_key_exists($key, $request)) {
return false;
}

if($request[$key] != $value) {
return false;
}
}

return true;
}


Where $request is an array of the current request's query params.



Is there a quicker way to do this with array functions that I'm missing?










share|improve this question























  • Would you expect it to return true on both of your examples? You'd like it to return true on the empty array too?

    – EarthDragon
    Jul 9 '17 at 5:33






  • 1





    isset( $request[$key] ) will be faster then the array_key_exists(), or at least it was a few years ago ... lol ... semantically I would combing the if's as well, if( !isset( $request[$key] ) || $request[$key] !== $value ) and use strict typing for things like false vs 0 if you care about those edge cases...

    – ArtisticPhoenix
    Jul 9 '17 at 5:35













  • You should also check array_intersect it gives you the intersection of your arrays

    – EarthDragon
    Jul 9 '17 at 5:38






  • 2





    @EarthDragon - that's a good idea but I would use array_intersect_assoc instead so the keys are checked.

    – ArtisticPhoenix
    Jul 9 '17 at 5:39











  • @EarthDragon, No, I'd want false on the second example. I looked at array_intersect_assoc, but I want to check that all the key/value pairs exist in both and that just leaves me with having to check the result, unless I'm missing something.

    – NicholasJohn16
    Jul 9 '17 at 5:42
















0












0








0








I need to check if a set of query params exists within a larger set of query params. For example, I want to check if array('option' => 'com_pages', 'view' => 'page'), exists within:



Array
(
[option] => com_pages
[format] => html
[view] => page
)


or



Array
(
[option] => com_archive
)


I tried to find a php function that would provide this kind of search, but could't find any that matched what I was looking for. So I created the below function:



function checkRoute($params, $request) {
foreach($params as $key => $value) {
if(!array_key_exists($key, $request)) {
return false;
}

if($request[$key] != $value) {
return false;
}
}

return true;
}


Where $request is an array of the current request's query params.



Is there a quicker way to do this with array functions that I'm missing?










share|improve this question














I need to check if a set of query params exists within a larger set of query params. For example, I want to check if array('option' => 'com_pages', 'view' => 'page'), exists within:



Array
(
[option] => com_pages
[format] => html
[view] => page
)


or



Array
(
[option] => com_archive
)


I tried to find a php function that would provide this kind of search, but could't find any that matched what I was looking for. So I created the below function:



function checkRoute($params, $request) {
foreach($params as $key => $value) {
if(!array_key_exists($key, $request)) {
return false;
}

if($request[$key] != $value) {
return false;
}
}

return true;
}


Where $request is an array of the current request's query params.



Is there a quicker way to do this with array functions that I'm missing?







php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 9 '17 at 5:29









NicholasJohn16NicholasJohn16

1,61611330




1,61611330













  • Would you expect it to return true on both of your examples? You'd like it to return true on the empty array too?

    – EarthDragon
    Jul 9 '17 at 5:33






  • 1





    isset( $request[$key] ) will be faster then the array_key_exists(), or at least it was a few years ago ... lol ... semantically I would combing the if's as well, if( !isset( $request[$key] ) || $request[$key] !== $value ) and use strict typing for things like false vs 0 if you care about those edge cases...

    – ArtisticPhoenix
    Jul 9 '17 at 5:35













  • You should also check array_intersect it gives you the intersection of your arrays

    – EarthDragon
    Jul 9 '17 at 5:38






  • 2





    @EarthDragon - that's a good idea but I would use array_intersect_assoc instead so the keys are checked.

    – ArtisticPhoenix
    Jul 9 '17 at 5:39











  • @EarthDragon, No, I'd want false on the second example. I looked at array_intersect_assoc, but I want to check that all the key/value pairs exist in both and that just leaves me with having to check the result, unless I'm missing something.

    – NicholasJohn16
    Jul 9 '17 at 5:42





















  • Would you expect it to return true on both of your examples? You'd like it to return true on the empty array too?

    – EarthDragon
    Jul 9 '17 at 5:33






  • 1





    isset( $request[$key] ) will be faster then the array_key_exists(), or at least it was a few years ago ... lol ... semantically I would combing the if's as well, if( !isset( $request[$key] ) || $request[$key] !== $value ) and use strict typing for things like false vs 0 if you care about those edge cases...

    – ArtisticPhoenix
    Jul 9 '17 at 5:35













  • You should also check array_intersect it gives you the intersection of your arrays

    – EarthDragon
    Jul 9 '17 at 5:38






  • 2





    @EarthDragon - that's a good idea but I would use array_intersect_assoc instead so the keys are checked.

    – ArtisticPhoenix
    Jul 9 '17 at 5:39











  • @EarthDragon, No, I'd want false on the second example. I looked at array_intersect_assoc, but I want to check that all the key/value pairs exist in both and that just leaves me with having to check the result, unless I'm missing something.

    – NicholasJohn16
    Jul 9 '17 at 5:42



















Would you expect it to return true on both of your examples? You'd like it to return true on the empty array too?

– EarthDragon
Jul 9 '17 at 5:33





Would you expect it to return true on both of your examples? You'd like it to return true on the empty array too?

– EarthDragon
Jul 9 '17 at 5:33




1




1





isset( $request[$key] ) will be faster then the array_key_exists(), or at least it was a few years ago ... lol ... semantically I would combing the if's as well, if( !isset( $request[$key] ) || $request[$key] !== $value ) and use strict typing for things like false vs 0 if you care about those edge cases...

– ArtisticPhoenix
Jul 9 '17 at 5:35







isset( $request[$key] ) will be faster then the array_key_exists(), or at least it was a few years ago ... lol ... semantically I would combing the if's as well, if( !isset( $request[$key] ) || $request[$key] !== $value ) and use strict typing for things like false vs 0 if you care about those edge cases...

– ArtisticPhoenix
Jul 9 '17 at 5:35















You should also check array_intersect it gives you the intersection of your arrays

– EarthDragon
Jul 9 '17 at 5:38





You should also check array_intersect it gives you the intersection of your arrays

– EarthDragon
Jul 9 '17 at 5:38




2




2





@EarthDragon - that's a good idea but I would use array_intersect_assoc instead so the keys are checked.

– ArtisticPhoenix
Jul 9 '17 at 5:39





@EarthDragon - that's a good idea but I would use array_intersect_assoc instead so the keys are checked.

– ArtisticPhoenix
Jul 9 '17 at 5:39













@EarthDragon, No, I'd want false on the second example. I looked at array_intersect_assoc, but I want to check that all the key/value pairs exist in both and that just leaves me with having to check the result, unless I'm missing something.

– NicholasJohn16
Jul 9 '17 at 5:42







@EarthDragon, No, I'd want false on the second example. I looked at array_intersect_assoc, but I want to check that all the key/value pairs exist in both and that just leaves me with having to check the result, unless I'm missing something.

– NicholasJohn16
Jul 9 '17 at 5:42














1 Answer
1






active

oldest

votes


















2














You could use an array_intersect_assoc for the two array and check if the count of the result is equal to the number of expected match...



 if ( count(array_intersect_assoc($a, $b)) == count($a) ){
// then match ...
}





share|improve this answer


























  • with .. == count($a) much better .. thanks .. @NicholasJohn16

    – scaisEdge
    Jul 9 '17 at 5:51











  • As suggested by @ArtisticPhoenix you should use array_intersect_assoc for comparing the keys too

    – EarthDragon
    Jul 9 '17 at 6:16











  • @EarthDragon .. correct answer updated

    – scaisEdge
    Jul 9 '17 at 6:18











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%2f44993288%2fcheck-if-key-value-pairs-exist-within-array%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









2














You could use an array_intersect_assoc for the two array and check if the count of the result is equal to the number of expected match...



 if ( count(array_intersect_assoc($a, $b)) == count($a) ){
// then match ...
}





share|improve this answer


























  • with .. == count($a) much better .. thanks .. @NicholasJohn16

    – scaisEdge
    Jul 9 '17 at 5:51











  • As suggested by @ArtisticPhoenix you should use array_intersect_assoc for comparing the keys too

    – EarthDragon
    Jul 9 '17 at 6:16











  • @EarthDragon .. correct answer updated

    – scaisEdge
    Jul 9 '17 at 6:18
















2














You could use an array_intersect_assoc for the two array and check if the count of the result is equal to the number of expected match...



 if ( count(array_intersect_assoc($a, $b)) == count($a) ){
// then match ...
}





share|improve this answer


























  • with .. == count($a) much better .. thanks .. @NicholasJohn16

    – scaisEdge
    Jul 9 '17 at 5:51











  • As suggested by @ArtisticPhoenix you should use array_intersect_assoc for comparing the keys too

    – EarthDragon
    Jul 9 '17 at 6:16











  • @EarthDragon .. correct answer updated

    – scaisEdge
    Jul 9 '17 at 6:18














2












2








2







You could use an array_intersect_assoc for the two array and check if the count of the result is equal to the number of expected match...



 if ( count(array_intersect_assoc($a, $b)) == count($a) ){
// then match ...
}





share|improve this answer















You could use an array_intersect_assoc for the two array and check if the count of the result is equal to the number of expected match...



 if ( count(array_intersect_assoc($a, $b)) == count($a) ){
// then match ...
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 19 at 23:19









NicholasJohn16

1,61611330




1,61611330










answered Jul 9 '17 at 5:45









scaisEdgescaisEdge

93.5k104970




93.5k104970













  • with .. == count($a) much better .. thanks .. @NicholasJohn16

    – scaisEdge
    Jul 9 '17 at 5:51











  • As suggested by @ArtisticPhoenix you should use array_intersect_assoc for comparing the keys too

    – EarthDragon
    Jul 9 '17 at 6:16











  • @EarthDragon .. correct answer updated

    – scaisEdge
    Jul 9 '17 at 6:18



















  • with .. == count($a) much better .. thanks .. @NicholasJohn16

    – scaisEdge
    Jul 9 '17 at 5:51











  • As suggested by @ArtisticPhoenix you should use array_intersect_assoc for comparing the keys too

    – EarthDragon
    Jul 9 '17 at 6:16











  • @EarthDragon .. correct answer updated

    – scaisEdge
    Jul 9 '17 at 6:18

















with .. == count($a) much better .. thanks .. @NicholasJohn16

– scaisEdge
Jul 9 '17 at 5:51





with .. == count($a) much better .. thanks .. @NicholasJohn16

– scaisEdge
Jul 9 '17 at 5:51













As suggested by @ArtisticPhoenix you should use array_intersect_assoc for comparing the keys too

– EarthDragon
Jul 9 '17 at 6:16





As suggested by @ArtisticPhoenix you should use array_intersect_assoc for comparing the keys too

– EarthDragon
Jul 9 '17 at 6:16













@EarthDragon .. correct answer updated

– scaisEdge
Jul 9 '17 at 6:18





@EarthDragon .. correct answer updated

– scaisEdge
Jul 9 '17 at 6:18


















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%2f44993288%2fcheck-if-key-value-pairs-exist-within-array%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







Popular posts from this blog

Callistus III

Plistias Cous

Index Sanctorum