SaranacLake Posted February 16, 2021 Share Posted February 16, 2021 (edited) I am trying to compare an array containing required questions to an array holding survey responses. When a user submits a survey, I take the $_POST array and write the values to a new array called $surveyResponseArray. How can I determine if $surveyResponseArray exists? Edited February 16, 2021 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/312165-check-if-array-exists/ Share on other sites More sharing options...
SaranacLake Posted February 16, 2021 Author Share Posted February 16, 2021 It looks like this might work? if (empty($myArray)){ // Handle array }else{ // Handle empty array } Quote Link to comment https://forums.phpfreaks.com/topic/312165-check-if-array-exists/#findComment-1584533 Share on other sites More sharing options...
gw1500se Posted February 16, 2021 Share Posted February 16, 2021 $_POST will normally always have values for each element in the form. However, some of the values may be "empty". So you would need to use 'empty' on each element rather than the entire array. As an aside, although it will not do what you want, based on the code comments your 'if' block should be: if (!empty($myArray)){ // Handle array }else{ // Handle empty array } 1 Quote Link to comment https://forums.phpfreaks.com/topic/312165-check-if-array-exists/#findComment-1584536 Share on other sites More sharing options...
SaranacLake Posted February 16, 2021 Author Share Posted February 16, 2021 @gw1500se Yes, I forgot the "!" empty() seems to do the trick. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/312165-check-if-array-exists/#findComment-1584538 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.