PHP code example of vildanhakanaj / php-options

1. Go to this page and download the library: Download vildanhakanaj/php-options library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vildanhakanaj / php-options example snippets


use VildanHakanaj\Options;

$options = new Options([
    "key1" => "value1",
    "key2" => "value2"
]);
// or
$options = Options::fromArray([
    "key1" => "value1",
    "key2" => "value2"
]);

// Set options
// Use magic setters
$options->key = "value";
// Merge with key value
$options->mergeKey("key", "value");
// Merge an array with key values
$options->merge(["key" => "value", "key1" => "value1"]);
// override options with the given array 
$options->override(["newKey" => "newValue"]);
// Only add if its not already in the options
$options->addIfUnique("key", "value");

// Any of the merge operations will override if any of the keys already exists in the options array.
// get value out of options
//Will return null if the key is not found
$value = $options->get("key");
$value = $options->key;
$value = $options["key"];
//Get all values
$values = $options->values();
//Get all keys
$keys = $options->keys();
//Get all options
$array = $options->all();
//Check if the key is in options array
$boolean = $options->has("key");

//Filter by value
$filteredOptions = $options->filter(function($option){
    return true; /*logic for filtering*/
});
// Remove the falsy values
$onlyTruthyValues = $options->filter();
//Filter by key
$filteredOptions = $options->filterByKey(function($option){
    return true; /*logic for filtering*/
});

foreach(Option::fromArray(["key" => "value"]) as $optionKey => $optionValue){
    // $optionKey = "key";
    // $optionValue = "value";
}
bash
composer