AlterArrayCleanPropertyTrait
Removes empty or null entries from an array value.
This alteration is typically declared with the Alter::CLEAN type and is used in
property transformation pipelines to discard "blank" elements (empty strings and
unset/null values) before the array is stored or further processed. It is often
chained after AlterArrayPropertyTrait to clean up an array that was just
built from a raw string:
Property::CATEGORY => [ Alter::ARRAY , Alter::CLEAN ] ,
Behavior details:
- If the value is an array, elements equal to Char::EMPTY (the empty string)
or not set are filtered out and the
$modifiedflag is set totrue. - If the value is not an array, it is returned untouched and
$modifiedis left as-is.
Note that the original keys are preserved by array_filter() (the array is not re-indexed).
Tags
Table of Contents
Methods
- alterArrayCleanProperty() : array<string|int, mixed>|float
- Removes empty-string and null elements from an array value.
Methods
alterArrayCleanProperty()
Removes empty-string and null elements from an array value.
public
alterArrayCleanProperty(mixed $value[, bool &$modified = false ]) : array<string|int, mixed>|float
Only arrays are processed; any other value is returned unchanged. When an array
is given, the $modified flag is set to true even if no element was actually
removed (the filtering pass is always considered an alteration).
Parameters
- $value : mixed
-
The value to clean. When it is an array, empty-string and unset elements are removed; otherwise it is returned as-is.
- $modified : bool = false
-
Reference flag set to
truewhen an array was processed.
Return values
array<string|int, mixed>|float —The cleaned array (keys preserved), or the original value when it was not an array.
Example:
use oihana\models\traits\alters\AlterArrayCleanPropertyTrait;
class Product
{
use AlterArrayCleanPropertyTrait;
}
$product = new Product();
$modified = false;
$tags = $product->alterArrayCleanProperty( [ 'php' , '' , 'models' , null ] , $modified );
// $tags === [ 0 => 'php' , 2 => 'models' ] (keys preserved)
// $modified === true
$scalar = $product->alterArrayCleanProperty( 'unchanged' , $modified );
// $scalar === 'unchanged'