bcamp1973 Posted April 21, 2006 Share Posted April 21, 2006 So, i have an array...[code]$arr = array( 'foo' => array('yada','yo','bing'), 'bar' => array('keep','it','simple','stupid'));[/code]and i want to check for the existence of a key in one of the nested arrays so i'm doing this...[code]$var1 = 'bar';$var2 = 'simple';if(isset($arr[$var1][$var2])) { echo '<p>Yup. Key '.$var2.' is in array '.$var1.'</p>';} else { echo '<p>nope, nada, zilch...it's just not there</p>';}[/code]Even when i put in values that are correct, i'm still getting my error message. Am i doing something wrong here? I don't get it... Link to comment https://forums.phpfreaks.com/topic/8091-check-for-existence-of-key-in-nested-array/ Share on other sites More sharing options...
Barand Posted April 22, 2006 Share Posted April 22, 2006 "simple" isn't a key, it's a value.It's key is $arr['bar'][2][code]Try$arr = array( 'foo' => array('yada','yo','bing'), 'bar' => array('keep','it','simple','stupid'));function inMyArray ($srch, &$arr) { foreach ($arr as $subarr) { if (in_array($srch, $subarr)) { return true; } } return false;}$s = 'simple';echo inMyArray($s, $arr) ? "found" : "not found";[/code] Link to comment https://forums.phpfreaks.com/topic/8091-check-for-existence-of-key-in-nested-array/#findComment-29557 Share on other sites More sharing options...
bcamp1973 Posted April 24, 2006 Author Share Posted April 24, 2006 Ahhh! ok....this is perfect...one question. what does the ampersand in "&$arr" accomplish? Link to comment https://forums.phpfreaks.com/topic/8091-check-for-existence-of-key-in-nested-array/#findComment-30135 Share on other sites More sharing options...
Barand Posted April 24, 2006 Share Posted April 24, 2006 [!--quoteo(post=368012:date=Apr 24 2006, 04:13 PM:name=bcamp1973)--][div class=\'quotetop\']QUOTE(bcamp1973 @ Apr 24 2006, 04:13 PM) [snapback]368012[/snapback][/div][div class=\'quotemain\'][!--quotec--]...one question. what does the ampersand in "&$arr" accomplish?[/quote]It passes the original array "by reference" instead of creating a copy of the array and passing that copy to the function. Link to comment https://forums.phpfreaks.com/topic/8091-check-for-existence-of-key-in-nested-array/#findComment-30199 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.