0

Hi all, I have a fairly simple problem. I have an array which prints out the following text:

1
 
Full, Gen. 3
 
TMSWK2D
9
Poor write quality
2
 
Full, Gen. 1
 
TMSWK2C
 
Read Only, Clean Tape

The problem is after some of the lines of text, there are empty values, causing there to be a line break. Here is the code that prints out the values.

<table>
<?php 
	$i=0;
	while(isset($html[$i]))
	{
?>
		<tr>
			<td><?php echo $html[$i]; ?></td>
		</tr>
<?php			
		$i++;
	}
?>
</table>

I have tried these two solutions in the hope that if one evaluates to true, then the $i++ would effectively skip the blank value and continue to print the next array value, but this hasn't worked.

if($html[$i] == " ")
		{
			$i++;
		}
	
		if(empty($html[$i]))
		{
			$i++;
		}

Any help please?!

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by mschroeder
0

I'm getting good at solving my own problems moments after I post them

<table>
<?php 
	$i=0;
	while(isset($html[$i]))
	{
		if(strlen($html[$i] == 0))
		{
			$i++;
		}
?>
		<tr>
			<td><?php echo $html[$i]; ?></td>
		</tr>
<?php			
		$i++;
	}
?>
</table>
0

Just an FYI, array_filter will also do just this when not supplied with a callback function. It will remove anything that when compared as a boolean evaluates to false. More examples in the menu.

<?php

$array = array(
	'1',
	'',
	'Full, Gen. 3',
	'',
	'TMSWK2D',
	'9',
	'Poor write quality',
	'2',
	'',
	'Full, Gen. 1',
	'',
	'TMSWK2C',
	'',
	'Read Only, Clean Tape',
);

var_dump($array);
$array = array_filter($array);
var_dump($array);
array
  0 => string '1' (length=1)
  1 => string '' (length=0)
  2 => string 'Full, Gen. 3' (length=12)
  3 => string '' (length=0)
  4 => string 'TMSWK2D' (length=7)
  5 => string '9' (length=1)
  6 => string 'Poor write quality' (length=18)
  7 => string '2' (length=1)
  8 => string '' (length=0)
  9 => string 'Full, Gen. 1' (length=12)
  10 => string '' (length=0)
  11 => string 'TMSWK2C' (length=7)
  12 => string '' (length=0)
  13 => string 'Read Only, Clean Tape' (length=21)
array
  0 => string '1' (length=1)
  2 => string 'Full, Gen. 3' (length=12)
  4 => string 'TMSWK2D' (length=7)
  5 => string '9' (length=1)
  6 => string 'Poor write quality' (length=18)
  7 => string '2' (length=1)
  9 => string 'Full, Gen. 1' (length=12)
  11 => string 'TMSWK2C' (length=7)
  13 => string 'Read Only, Clean Tape' (length=21)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.