PDA

View Full Version : How to prevent empty array values in serialization?


Lionel
01-06-2006, 08:21 PM
I have six textboxes on a page.

For the db insert I do

$preddata1 = serialize((is_array($_POST['predminutes1']) ? $_POST['predminutes1']: array()));

and I insert $preddata1.

Results inserted in db are as below

a:6:{s:10:"0000000031";s:2:"25";s:10:"0000000032";s:0:"";s:10:"0000000033";s:2:"35";s:10:"0000000034";s:0:"";s:10:"0000000035";s:0:"";s:10:"0000000036";s:0:"";}

As you can see the array only has 2 values "25" and "35".

How can I prevent the other empty values to be recorded?

akanevsky
01-07-2006, 11:17 AM
Unset empty array items?

Lionel
01-07-2006, 12:28 PM
how would i do that?

This is the full code
if (!empty($_POST['predminutes1'])){
$keys1 = array_keys($_POST['predminutes1']);
if (!empty($_POST['predplayer1'])){
$keys2 = array_keys($_POST['predplayer1']);
$diff = array_diff($keys1, $keys2);
if(count($diff) > 0) {
eval(print_standard_error('error_invalidpenalty1') );
}
}
}

Of course this is getting messed up since I am getting all the empties in $keys1 which only has 2 values

Here is a print out of results. First line is $keys1 second is $keys2 and third is $diff


Array ( [0] => 0000000031 [1] => 0000000032 [2] => 0000000033 [3] => 0000000034 [4] => 0000000035 [5] => 0000000036 )
Array ( [0] => 0000000034 [1] => 0000000036 )
Array ( [0] => 0000000031 [1] => 0000000032 [2] => 0000000033 [4] => 0000000035 )


All I want to say is that if the textbox($keys1) next to a checkbox($keys2) is empty (both have the same $playerid as key), eval the error.

filburt1
01-07-2006, 03:31 PM
foreach ($array as $key => $value)
{
if (empty($value) and $value !== 0 and $value !== false)
{
unset($array[$key]);
}
}

Lionel
01-07-2006, 03:40 PM
Thanks filburt1. Finally that is getting somewhere.