Page 1 of 1

Quick questions about manipulating arrays

Posted: Wed Jun 07, 2006 6:11 pm
by daedalus__
I really, normally wouldn't post something like this, but.. It's for work and I need it now.

Why isn't this working how I intend it to?

I want it to clear the empty keys out of the POST array. Is it because it's the post array? Is unset not the proper function here? Or am I simply an idiot?

Please help :(

Code: Select all

<?php
if ($_POST)
{
	echo '<h2>before:</h2>';
	echo "<pre>";
	print_r($_POST);
	echo "</pre>";
	
	function parse_array($item)
	{
		if (empty($item) != FALSE)
		{
			unset($item);
		}
	}
	
	array_walk($_POST, 'parse_array');

	echo '<h2>after:</h2>';
	echo "<pre>";
	print_r($_POST);
	echo "</pre>";
}
else
{
	?>	
	<form action="test.php" method="post">
	<?php
	for ($i = 0; $i < 15; $i++)
	{
		echo "<input type=\"input\" name=\"qty_".$i."\" size=\"5\" /><br />";
	}
	?>
	<input type="submit" name="submit" value="submit" />
	</form>
<?php
}
?>

Posted: Wed Jun 07, 2006 6:15 pm
by PrObLeM
why not?

Code: Select all

echo '<h2>before:</h2>';
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
      
foreach ($_POST as $k => $v) {
     if(empty($v)) {
          unset($_POST[$k]);
     }
}
 echo '<h2>after:</h2>';
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";

Posted: Wed Jun 07, 2006 6:51 pm
by daedalus__
Because it's easy, I just don't have time right now, lol.

I thought I would try that function though.

My brain is all over the place today >.<