How to get entry value by label

The following function can be used to get a field’s value from an entry, using the label as the key.

That is, if you know the label of the field (instead of the id) you can use this function to get the value.

function itsg_get_value_by_label( $form, $entry, $label ) {
	foreach ( $form['fields'] as $field ) {
		$lead_key = $field->label;
		if ( strToLower( $lead_key ) == strToLower( $key ) ) {
			return $entry[ $field->id ];
		}
	}
	return false;
}

Usage

For example, if you were in a situation where you have access to the form object ($form) and entry object ($entry) and need to get the value of a field, but only know the label of the field – for example, you’re querying multiple forms.

If you had a field with a label of “Do you like to eat bread?”

$eats_bread = itsg_get_value_by_label( $form, $entry, "Do you like to eat bread?" );

If the form has a field with this label, and the entry contains a value – it will return the value, otherwise it returns false.

Was this article helpful?

Related Articles