Need to ignore empty variable values for Computed Field when multiplying In my entityform, different fields appear based on choices (dependencies). Two appear at once. I'm multiplying the values using a Computed field. All works if the variables have a value. I have 8 fields I want to multiply, but ignore the empty values. Can't set field defaults to 0 (wrecks multiplication) or 1 (messes with totals).

The following warning appears for each empty field; here it's "line 1" for the 1st variable/field. No warning for those that have a value.


  Warning: array_pop() expects parameter 1 to be array, null given in eval() (line 1 of /home/content/67/10793667/html/sites/all/modules/computed_field/computed_field.module(466) : eval()'d code).


How can I instruct the computed field to ignore the empty fields?

Here is my Computed Code (PHP).

$howmany = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_how_many')));
$price1 = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_reg_price1')));
$price2 = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_sale_price2')));
$entity_field[0]['value'] = $howmany*$price1*$price2;


I tried adding this after the $price2 line, but it didn't work.

if ($price2 == null) {
    $price2 = 1;
}


I tried this too, and it didn't work.

if (!empty($price2)) {
  $price2 = 1;
}
else {
  $price2 = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_sale_price2')));
}
Both @Neo's answer and @danielesus's question shows PHP code that will throw errors. @Neo, empty() only works with variables before PHP 5.5. Both: array_pop requires a variable or something returned by a reference-returning-function which field_get_items is not.

Now let's consider what field_get_items($entity_type, $entity, 'field_how_many') returns: it returns an array indexed by delta where the values are field_column  =&gt; value. You can do

if ($all_items = field_get_items($entity_type, $entity, 'field_how_many')) {
  $item = reset($all_items);
  $value = reset($item);
}


as there's no need to actually modify the array with array_pop but also you can use your knowledge about Field API and know the actual column name which I suspect will be value here (would be tid for a taxonomy reference field for example) and that $delta is, if you have a field with cardinality 1 is actually 0:

$value = NULL;
if ($all_items = field_get_items($entity_type, $entity, 'field_how_many')) {
  $value = $all_items[0]['value'];
}


Fast and simple.