stoggafu Posted September 16, 2017 Share Posted September 16, 2017 I would like this to echo the $val['value'] if it has a value, and to display the number 0 if it does not. I have tried a few different ways of trying to get it to show 0 for the ones that have no value, but cannot seem to get both to work conditionally...Thank you <?php foreach($_44 as $val) if ($val['feat'] == '7mr') { echo $val['value']; } ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted September 16, 2017 Share Posted September 16, 2017 What does "no value" mean? Does not exist in the array at all? Exists but the value is an empty string? Value is null? Something else? 1 Quote Link to comment Share on other sites More sharing options...
stoggafu Posted September 16, 2017 Author Share Posted September 16, 2017 What does "no value" mean? Does not exist in the array at all? Exists but the value is an empty string? Value is null? Something else? Â Â Value does not exist. Thank you. Quote Link to comment Share on other sites More sharing options...
Sepodati Posted September 16, 2017 Share Posted September 16, 2017 (edited) Use isset(). Â echo (isset($val['value'])) ? $val['value'] : 0; Edited September 16, 2017 by Sepodati Quote Link to comment Share on other sites More sharing options...
stoggafu Posted September 16, 2017 Author Share Posted September 16, 2017 I tried this, but it is not giving me any output when the value does not exist, only when it exists.. <?php foreach($_44 as $val) { if ($val['feat'] == '7mr'){ echo isset($val['value']) && !empty($val['value']) ? $val['value'] : '0'; } } ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted September 16, 2017 Share Posted September 16, 2017 Works for me. Quote Link to comment Share on other sites More sharing options...
stoggafu Posted September 16, 2017 Author Share Posted September 16, 2017 Works for me. Â What if is there is no ['feat'] > ['value'] at all for return? Would I need to write a separate if? Quote Link to comment Share on other sites More sharing options...
requinix Posted September 16, 2017 Share Posted September 16, 2017 Maybe. Can't say for sure what you should do since I have no idea what the $_44 array is or what the "feat" and "value" mean. Maybe it's an error for it to not exist. Maybe the array is empty to begin with. Maybe something else. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2017 Share Posted September 16, 2017 The way to write the conditions is to decide what the conditions are first then start to code them, as opposed to the method you are asking us to take - write some conditions then introduce others once that is done. Quote Link to comment 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.