Quantcast
Channel: How to check if multiple array keys exists - Stack Overflow
Browsing latest articles
Browse All 22 View Live
↧

Answer by Ravi Shankar S K for How to check if multiple array keys exists

$colsRequired = ["apple", "orange", "banana", "grapes"]; $data = ["apple"=>"some text", "orange"=>"some text"]; $presentInBoth = array_intersect($colsRequired,array_keys($data)); if(...

View Article


Answer by jerryurenaa for How to check if multiple array keys exists

I usually use a function to validate my post and it is an answer for this question too so let me post it.to call my function I will use the 2 array like thisvalidatePost(['username', 'password', 'any...

View Article


Answer by Steve for How to check if multiple array keys exists

I use something like this quite often$wantedKeys = ['story', 'message'];$hasWantedKeys = count(array_intersect(array_keys($source), $wantedKeys)) > 0or to find the values for the wanted...

View Article

Answer by Casper Wilkes for How to check if multiple array keys exists

This is old and will probably get buried, but this is my attempt.I had an issue similar to @Ryan. In some cases, I needed to only check if at least 1 key was in an array, and in some cases, all needed...

View Article

Answer by K-Alex for How to check if multiple array keys exists

Hope this helps:function array_keys_exist($searchForKeys = array(), $inArray = array()) { $inArrayKeys = array_keys($inArray); return count(array_intersect($searchForKeys, $inArrayKeys)) ==...

View Article


Answer by Oluwatobi Samuel Omisakin for How to check if multiple array keys...

Something as this could be used//Say given this array$array_in_use2 = ['hay' => 'come', 'message' => 'no', 'story' => 'yes'];//This gives either true or false if story and message is...

View Article

Answer by iamandrewluca for How to check if multiple array keys exists

$myArray = array('key1' => '', 'key2' => '');$keys = array('key1', 'key2', 'key3');$keyExists = count(array_intersect($keys, array_keys($myArray)));Will return true, because there are keys from...

View Article

Answer by j4r3k for How to check if multiple array keys exists

// sample data$requiredKeys = ['key1', 'key2', 'key3'];$arrayToValidate = ['key1' => 1, 'key2' => 2, 'key3' => 3];function keysExist(array $requiredKeys, array $arrayToValidate) { if...

View Article


Answer by iautomation for How to check if multiple array keys exists

The above solutions are clever, but unnecessarily slow. A simple foreach loop over a few keys is much faster.function array_keys_exist($keys, $array){ foreach($keys as $key){ if(!array_key_exists($key,...

View Article


Answer by UpVs for How to check if multiple array keys exists

One more possible solution:if (!array_diff(['story', 'message'], array_keys($array))) { // OK: all the keys are in $array} else { // FAIL: some keys are not}

View Article

Answer by valmayaki for How to check if multiple array keys exists

try this$required=['a','b'];$data=['a'=>1,'b'=>2];if(count(array_intersect($required,array_keys($data))>0){ //a key or all keys in required exist in data }else{ //no keys found }

View Article

Answer by David Dutkovsky for How to check if multiple array keys exists

What about this: isset($arr['key1'], $arr['key2']) only return true if both are not nullif is null, key is not in array

View Article

Answer by Code Lover for How to check if multiple array keys exists

I am not sure, if it is bad idea but I use very simple foreach loop to check multiple array key.// get post attachment source url$image = wp_get_attachment_image_src(get_post_thumbnail_id($post_id),...

View Article


Answer by Vuong for How to check if multiple array keys exists

<?phpfunction check_keys_exists($keys_str = "", $arr = array()){ $return = false; if($keys_str != "" and !empty($arr)){ $keys = explode(',', $keys_str); if(!empty($keys)){ foreach($keys as $key){...

View Article

Answer by Petr Cibulka for How to check if multiple array keys exists

It seems to me, that the easiest method by far would be this:$required = array('a','b','c','d');$values = array('a' => '1','b' => '2');$missing = array_diff_key(array_flip($required),...

View Article


Answer by Mark Fox for How to check if multiple array keys exists

Surprisingly array_keys_exist doesn't exist?! In the interim that leaves some space to figure out a single line expression for this common task. I'm thinking of a shell script or another small...

View Article

Answer by Erfan for How to check if multiple array keys exists

Here is a solution that's scalable, even if you want to check for a large number of keys:<?php// The values in this arrays contains the names of the indexes (keys) // that should exist in the data...

View Article


Answer by Kim Stacks for How to check if multiple array keys exists

This is the function I wrote for myself to use within a class.<?php/** * Check the keys of an array against a list of values. Returns true if all values in the list is not in the array as a key....

View Article

Answer by Sven for How to check if multiple array keys exists

If you have something like this:$stuff = array();$stuff[0] = array('story' => 'A story', 'message' => 'in a bottle');$stuff[1] = array('story' => 'Foo');You could simply count():foreach...

View Article

Answer by Kiwi for How to check if multiple array keys exists

Does this not work?array_key_exists('story', $myarray) && array_key_exists('message', $myarray)

View Article

Answer by alex for How to check if multiple array keys exists

If you only have 2 keys to check (like in the original question), it's probably easy enough to just call array_key_exists() twice to check if the keys exists.if (array_key_exists("story", $arr)...

View Article


How to check if multiple array keys exists

I have a variety of arrays that will either containstory & messageor just storyHow would I check to see if an array contains both story and message? array_key_exists() only looks for that single...

View Article

Browsing latest articles
Browse All 22 View Live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>