Quick questions about manipulating arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Quick questions about manipulating arrays

Post 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
}
?>
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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>";
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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 >.<
Post Reply