Asked  7 Months ago    Answers:  5   Viewed   29 times

I'm adding an array of items from a form and if all of them are empty, I want to perform some validation and add to an error string. So I have:

$array = array(
    'RequestID'       => $_POST["RequestID"],
    'ClientName'      => $_POST["ClientName"],
    'Username'        => $_POST["Username"],
    'RequestAssignee' => $_POST["RequestAssignee"],
    'Status'          => $_POST["Status"],
    'Priority'        => $_POST["Priority"]

And then if all of the array elements are empty perform:

$error_str .= '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>';



You can just use the built in array_filter

If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.

So can do this in one simple line.

if(!array_filter($array)) {
    echo '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>';
Wednesday, March 31, 2021
answered 7 Months ago

You could use this to recursively determine whether all required keys are present:

function has_recursive($data, $required)
    foreach ($required as $key => $value) {
        if (!isset($data[$key])/* && $data[$key] === $value */) {
            return false;
        if (is_array($data[$key]) && false === has_recursive($data[$key], $value)) {
            return false;
    return true;

has_recursive($data, $required); // false or true
Wednesday, March 31, 2021
answered 7 Months ago

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;

    public function bindValue(PHPExcel_Cell $cell, $value = null)
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
Friday, May 28, 2021
answered 5 Months ago
if(max($yourArray) < 5) {
  //all values in array are less than 5
Saturday, May 29, 2021
answered 5 Months ago

You can use Where in LINQ to select strings which are not empty:

 return string.Join("n", parts.Where(s => !string.IsNullOrEmpty(s)));
Sunday, August 22, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :