Click here to Skip to main content
16,004,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having an array of input named "date" returning 11 values of which only 6 are set with date values. My aim is to make sure all the remaining 7 empty values in the array are unset from the arrays, to be reindexed with only the set date values.
Despite my trials, the reindexed array still returns 2 empty values along with the 6 set ones, giving me an array with 8 values. I've tried strlen, empty, isset and even checking if the values in array are dates without much avail, it still gives me the same result. Your help on this issue will be greatly welcomed, Thanks !

What I have tried:

<?php



$date = $_GET['date'];




//To not take into account any empty date
for ($i = 0; $i < sizeof($date); $i++) {
    if ($date[$i] == '') {
        unset($date[$i]);
        
    }
    
    $date = array_values($date);
    
    
    
}


//displaying this shows 2 empty values with the 6 set values
for ($i = 0; $i < sizeof($date); $i++) {
    echo "<li>";
    echo $date[$i];
    echo "</li>";
}

?>
Posted
Updated 15-Nov-17 1:05am

How about insert the non empty date into a different array and then print? Something like this.

PHP
<?php

$date = array("11/11/2017","12/11/2017","13/11/2017","14/11/2017","15/11/2017","16/11/2017","","","","","");
$date_NoEmpty = $date;
//To not take into account any empty date
for ($i = 0; $i < sizeof($date); $i++) {
   // echo sizeof($date) . ' ' .  $i . '<br/>';
    if ($date[$i] == '') {
        unset($date_NoEmpty[$i]);
       // echo 'empty';
    }
   // echo $date[$i];
  //  $date = array_values($date);
}

//displaying this shows 2 empty values with the 6 set values
for ($i = 0; $i < sizeof($date_NoEmpty); $i++) {
    echo "<li>";
    echo $date_NoEmpty[$i];
    echo "</li>";
}

?>

Output:
11/11/2017
12/11/2017
13/11/2017
14/11/2017
15/11/2017
16/11/2017
 
Share this answer
 
Comments
Member 13471028 15-Nov-17 7:04am    
Thanks, your answer contributed in the solution I found alot !
PHP
$date = array("11/11/2017","12/11/2017","13/11/2017","14/11/2017","15/11/2017","16/11/2017"," "," ","","");
print_r(array_filter($date, function($value) { return trim($value) !== ''; }));

Maybe the problem is that the values aren't empty, but have a space in there? This function should get rid of that for you.
 
Share this answer
 
v2
Comments
Member 13471028 15-Nov-17 7:04am    
Thanks, your comment helped me alot in finding the solution
<?php
$date = $_GET['date'];
$check = $date;

// Check helps us to get the real non empty dates

for ($i = 0; $i < sizeof($check); $i++)
	{
	if ($check[$i] == '')
		{
		unset($check[$i]);
		}

	$check = array_values($check);
	}

// if the sizeof CHECK is equal the size of DATE INPUT it means everything went well with no trailing empty problems
// Also that it is the first time that an empty table is passed

if (sizeof(array_filter($date,
function ($value)
	{
	return trim($value) !== '';
	})) == sizeof($check))
	{
	for ($i = 0; $i < sizeof($date); $i++)
		{
		if ($date[$i] == '')
			{
			unset($date[$i]);
			}

		$date = array_values($date);
		}
	}

// If it is not equal it means trailing empty spaces are there
// Also that is not the first time empty table is passed

  else
if (sizeof(array_filter($date,
function ($value)
	{
	return trim($value) !== '';
	})) != sizeof($check))
	{
	$date = array_filter($date,
	function ($value)
		{
		return trim($value) !== '';
		});
	$date = array_values($date);
	}

?>
 
Share this answer
 
Just a word of caution - what exactly is 'empty' ?
For example, what if it is NULL ?

There are other ways to check in php that allow you quite a bit of control as to what you call empty.

Examples are is_null() and empty() . There are others with various nuances. It would be good for you to read about them - so they're stored in your mind for future reference.

PHP isset() vs empty() vs is_null() - Virendra's TechTalk[^]

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900