0

I'm new to PHP and I tried to print array values vertically but it shows values horizontally. This is the code that I tried. Please help me, guys.

$roomCount = count($_POST["room_nos"]);
    for($i=0;$i<$roomCount;$i++) {
        foreach($roomsCount as $value){
        
}
        $output.= $roomsCount[$i];
}

echo $output;

Current output is 120122.

But I want it as

120
122

Really waiting for your help.

4
  • 2
    You can use \n to break line. $output.= $roomsCount[$i] . " \n"; Commented Oct 24, 2020 at 20:08
  • 1
    @NícolasSimsBotelho has you on the right tack, though it might be better to use $output.= $roomsCount[$i] . PHP_EOL;, or, if you're looking to output this in a browser, you might want $output.= $roomsCount[$i] . '<br>'; What you use might depend on context, but the main point is that you need to tell your program to output each value on a new line. Commented Oct 24, 2020 at 20:17
  • 1
    I agree with you @WesleySmith, thank you. Commented Oct 24, 2020 at 21:27
  • Thank you for the help. I want to display it on the browser. This works. $output.= $roomsCount[$i] . '<br>'; Commented Oct 29, 2020 at 11:45

4 Answers 4

1

You can use \n to break line. $output.= $roomsCount[$i] . " \n"; Or as @WesleySmith said: it might be better to use $output.= $roomsCount[$i] . PHP_EOL;, or, if you're looking to output this in a browser, you might want $output.= $roomsCount[$i] . '<br>';

Sign up to request clarification or add additional context in comments.

Comments

1

You need to add break line to your code. there is some ways which i know and you have to use them on where needed for example using <br> is ok for html but might not gonna work if you are not using php in html codes.

For using in html try this edited code :

$roomCount = count($_POST["room_nos"]);
    for($i=0;$i<$roomCount;$i++) {
        foreach($roomsCount as $value){
        
}
        $output.= $roomsCount[$i]."<br>";
        // edited over here // ."<br>" added to your code
}

echo $output;

For using in JavaScript or jQuery string returns or console.log or other things you can use this code too :

$roomCount = count($_POST["room_nos"]);
    for($i=0;$i<$roomCount;$i++) {
        foreach($roomsCount as $value){

}
        $output.= $roomsCount[$i]."\n";
        // edited over here // ."\n" added to your code
}

echo $output;

Also if you are trying to save it on database or using on any API you might need to use this code PHP_EOL like this :

$roomCount = count($_POST["room_nos"]);
    for($i=0;$i<$roomCount;$i++) {
        foreach($roomsCount as $value){

}
        $output.= $roomsCount[$i].PHP_EOL;
        // edited over here // .PHP_EOL added to your code
}

echo $output;

I not sure its all things which you can use but its all codes which i knew i hope its resolve your problem.

2 Comments

One further option, for what its worth, return JSON string containing just the array of numbers, then, let the front end decide how to display it.
Great. Really thank you for all of your solutions.
0

insert a line break :

    $output.= sprintf ("%d<br>",$roomsCount[$i]); 

3 Comments

only if being outputted into html though. If for example this is outputted into a textarea tag, this would fail.
also, useing sprintf here seems a bit like overkill when concatenation would be simpler
yes. replace the "<br>" by "\n" for the textarea
-1

you can also append <pre></pre> html tag to print horizontally in php.

 $output .=  '<pre>' . $output .'</pre>';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.