1

let's say I want to construct some input fields using this loop

    $array = [];
        foreach ( $array as $key => $value ) {
        echo '<input type="text" value="'. $value .'" />';
    }

In this particular case, this will not echo anything cause the array is empty, but I want to print the input field one time with no value if the array is empty. How can I accomplish this?

silvered.dragon
  • 407
  • 1
  • 7
  • 19
  • 1
    `};` the `;` is not required – RiggsFolly Aug 26 '20 at 14:07
  • 1
    Check if your array is empty before the loop, and if so, then _fill it_ with one item at this point, so that your loop actually _has_ something to loop over. – CBroe Aug 26 '20 at 14:09
  • Does this answer your question? [How to check whether an array is empty using PHP?](https://stackoverflow.com/questions/2216052/how-to-check-whether-an-array-is-empty-using-php) – Timberman Aug 26 '20 at 14:10
  • 1
    Ermmm... would `$array = [""];` or `$array = $array ?? [""];` do the job? – Nigel Ren Aug 26 '20 at 14:13

2 Answers2

1

If you don’t want to repeat the code you have in the loop elsewhere - then you need to give the loop something to loop over, if you want that code inside to execute, simple as that.

$array = [];

if(!$array) { // array is empty
  $array = ['']; // so *make it* an array with one item, an empty string
}

foreach ( $array as $key => $value ) {
  echo '<input type="text" value="'. $value .'" />';
};

Edit:

foreach ( ($array ? $array : ['']) as $key => $value )

This would also do the job, if one considers that any “nicer” looking.

(Would still error if $array was a non-falsey non-array value to begin with though.)

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • is it possible to use something like a ternary operator inside the foreach parameters? – silvered.dragon Aug 26 '20 at 14:14
  • This will check if the variable is false, not particually if the array is empty – Timberman Aug 26 '20 at 14:15
  • 2
    I would think if the variable is falsey, then it means its either an empty array, or not created right in the first place (which means there are bigger troubles to worry about haha). So I consider this legit enough as it basically self-mends the situation for the foreach (as passing a non-array to the foreach would cause an error). Certainly not a downvoteworthy situation either way. – IncredibleHat Aug 26 '20 at 14:19
  • 1
    @Timberman I specifically based that check on https://stackoverflow.com/questions/2216052/how-to-check-whether-an-array-is-empty-using-php, which you yourself suggested as a duplicate for this question here. But yes, that can be replaced with `if(!count($array))`, if you like that any better. – CBroe Aug 26 '20 at 14:28
  • @silvered.dragon well `foreach ( ($array ? $array : ['']) as $key => $value )` would work, if you think that’s any better. – CBroe Aug 26 '20 at 14:31
  • oooh with the ternary solution it looks perfect(you need () around this is why wasn't working to me), I don't care about the non-array value since I'm obtaining this array from the wordpress get_option() that is always returning an array from json data. Thank you – silvered.dragon Aug 26 '20 at 14:38
0
if(count($array) == 0)
{
    echo '<input type="text" value="" />';
}

OR:

if(empty($array))
{
    echo '<input type="text" value="" />';
}
Timberman
  • 647
  • 8
  • 24
  • is it possible to use something like a ternary operator inside the foreach parameters? – silvered.dragon Aug 26 '20 at 14:14
  • 1
    @silvered.dragon Not inside of the foreach, since that your array is empty it wont even run once. You have to do that outside of the foreach – Timberman Aug 26 '20 at 14:16
  • Welcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users with similar issues. – FluffyKitten Aug 27 '20 at 02:16
  • @FluffyKitten OP asked how to check for an empty array. Not sure how much explaining there is to do – Timberman Aug 27 '20 at 08:12