tqla Posted February 2, 2013 Share Posted February 2, 2013 I have a session array that is added to by a form. Here it is when the form is submitted twice. It can grow larger and larger in a single session but the keys are always the same. What I need to do is check all the arrays to see if the key called Form matches a variable and if it matches then I will use that values in that particular array. array 0 => array 'Age' => string '20' (length=2) 'City' => string 'New York' (length= 'Your_name' => string 'Jim' (length=3) 'Friend1' => string 'Joseph' (length=6) 'Friend2' => string 'Waldo' (length=5) 'Form' => string 'help' (length=4) 1 => array 'Age' => string '25' (length=2) 'City' => string 'Sun Valley' (length=10) 'Your_name' => string 'Susan' (length=5) 'Friend1' => string 'Rick' (length=4) 'Friend2' => string 'Joe' (length=3) 'Form' => string 'welcome' (length=4) I know that I can do something like this to check the first one. $key = 'help'; $array = $_SESSION['array']; if($array[0]['Form'] == $key){ echo "We have a match"; } But how can I automatically cycle through [0], [1], and so on until I find a match? Thank you. Link to comment https://forums.phpfreaks.com/topic/273934-array-help/ Share on other sites More sharing options...
requinix Posted February 2, 2013 Share Posted February 2, 2013 If the "Form" is a unique string then why not use it as the array key instead of the numbers? array 'help' => array 'Age' => string '20' (length=2) 'City' => string 'New York' (length= 'Your_name' => string 'Jim' (length=3) 'Friend1' => string 'Joseph' (length=6) 'Friend2' => string 'Waldo' (length=5) 'Form' => string 'help' (length=4) 'welcome' => array 'Age' => string '25' (length=2) 'City' => string 'Sun Valley' (length=10) 'Your_name' => string 'Susan' (length=5) 'Friend1' => string 'Rick' (length=4) 'Friend2' => string 'Joe' (length=3) 'Form' => string 'welcome' (length=4) $key = 'help'; $array = $_SESSION['array']; if(isset($array[$key])){ echo "We have a match"; } Link to comment https://forums.phpfreaks.com/topic/273934-array-help/#findComment-1409639 Share on other sites More sharing options...
tqla Posted February 2, 2013 Author Share Posted February 2, 2013 Great suggestion! Thank you requinix! I made that change and it worked out. Link to comment https://forums.phpfreaks.com/topic/273934-array-help/#findComment-1409644 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.