$options = [ ['id' => 1, 'name' => 'Option 1', 'value' => 'val1'], ['id' => 2, 'name' => 'Option 2', 'value' => 'val2'], ['id' => 3, 'name' => 'Option 3', 'value' => 'val1'], ['id' => 4, 'name' => 'Option 4', 'value' => 'val3'], ]; $filtered = OptionArray::filter($options, function($o) { return $o['value'] === 'val1'; }); // $filtered would now contain: // [ // ['id' => 1, 'name' => 'Option 1', 'value' => 'val1'], // ['id' => 3, 'name' => 'Option 3', 'value' => 'val1'], // ]
$options = [ ['id' => 1, 'name' => 'Option 1', 'value' => 'val1'], ['id' => 2, 'name' => 'Option 2', 'value' => 'val2'], ['id' => 3, 'name' => 'Option 3', 'value' => 'val1'], ['id' => 4, 'name' => 'Option 4', 'value' => 'val3'], ]; $sorted = OptionArray::sort($options, function($a, $b) { return $a['name'] > $b['name']; }); // $sorted would now contain: // [ // ['id' => 1, 'name' => 'Option 1', 'value' => 'val1'], // ['id' => 3, 'name' => 'Option 3', 'value' => 'val1'], // ['id' => 2, 'name' => 'Option 2', 'value' => 'val2'], // ['id' => 4, 'name' => 'Option 4', 'value' => 'val3'], // ]
$options = [ ['id' => 1, 'name' => 'Option 1', 'value' => 'val1'], ['id' => 2, 'name' => 'Option 2', 'value' => 'val2'], ['id' => 3, 'name' => 'Option 3', 'value' => 'val1'], ['id' => 4, 'name' => 'Option 4', 'value' => 'val3'], ]; $transformed = OptionArray::map($options, function($o) { return [ 'id' => $o['id'], 'text' => $o['name'] . ' (' . $o['value'] . ')', ]; }); // $transformed would now contain: // [ // ['id' => 1, 'text' => 'Option 1 (val1)'], // ['id' => 2, 'text' => 'Option 2 (val2)'], // ['id' => 3, 'text' => 'Option 3 (val1)'], // ['id' => 4, 'text' => 'Option 4 (val3)'], // ]Overall, the OptionArray utility in PHP is a convenient package library for working with arrays containing options, and can be easily used to filter, sort, or transform options in a variety of ways.