phpQuestioner Posted October 28, 2007 Share Posted October 28, 2007 How can I detect when an array has an empty value? Here is what I have so far. <?php $products[] = ""; // This Works If The Above Array Does Not Exist At All // But It Will Not Work; If The Above Array Is Present With An Empty Value if (array_count_values($products) == 0) { $products[] = "Item Not Available"; } foreach($products as $key => $value) { echo "$products<br>"; } ?> I tried this; without an results: <?php $products[] = ""; // This Works If The Above Array Does Not Exist At All // But It Will Not Work; If The Above Array Is Present With An Empty Value if (array_count_values($products) == 0) { $products[] = "Item Not Available"; } else if ($products == NULL) { $products[] = "Item Not Available"; } foreach($products as $key => $value) { echo "$products<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75073-detecting-an-empty-array/ Share on other sites More sharing options...
LemonInflux Posted October 28, 2007 Share Posted October 28, 2007 <?php $products[] = ""; // This Works If The Above Array Does Not Exist At All // But It Will Not Work; If The Above Array Is Present With An Empty Value if (!$products) { $products[] = "Item Not Available"; } foreach($products as $key => $value) { echo "$products<br>"; } ?> like that? Sorry, I don't use arrays if I don't have to. Quote Link to comment https://forums.phpfreaks.com/topic/75073-detecting-an-empty-array/#findComment-379689 Share on other sites More sharing options...
phpQuestioner Posted October 28, 2007 Author Share Posted October 28, 2007 LemonInflux - That did not work - Thanks Anyways Any other ideas on how to accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/75073-detecting-an-empty-array/#findComment-379690 Share on other sites More sharing options...
recklessgeneral Posted October 28, 2007 Share Posted October 28, 2007 Hi, The problem is that you're initializing the array with a value, then checking whether there are any values in the array, which will return true or 1 or whatever. What you can do instead is initialize $products to an empty array, and I used the count function instead of array_count_values: <?php $products = array(); // This Works If The Above Array Does Not Exist At All // But It Will Not Work; If The Above Array Is Present With An Empty Value if (count($products) == 0) { $products[] = "Item Not Available"; } foreach($products as $key => $value) { echo "$value<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75073-detecting-an-empty-array/#findComment-379695 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.