PHP code example of bkvfoundry / utility-belt

1. Go to this page and download the library: Download bkvfoundry/utility-belt 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/ */

    

bkvfoundry / utility-belt example snippets


$items = [
    [
        "name"=>"john",
        "age"=>18
    ],
    [
        "name"=>"mary",
        "age"=>19
    ],
    [
        "name"=>"william",
        "age"=>18,
        "dog"=>[
            "name"=>"betty"
        ]
    ]
];

CollectionUtility::filterWhere($items,["age"=>18])
// [["name"=>"john",...], ["name"=>"william",...]];

CollectionUtility::filterWhereNot($items,["age"=>18])
// [["name"=>"mary",...]]

CollectionUtility::filterWhere($items,["dog.name"=>"betty"])
// [["name"=>"william",...]]

CollectionUtility::filterWhere($items,[["dog.name"=>"betty"],["name"=>"john"]]);
// [["name"=>"william",...], ["name"=>"john"]]

$items = [
    ["name"=>"john","dog"=>["name"=>"william"]],
    ["name"=>"frank","dog"=>["name"=>"william"]],
    ["name"=>"dodd","dog"=>["name"=>"bruce"]],
];

CollectionUtility::keyByProperty($items,"dog.name")
[
   "william"=>["name"=>"frank","dog"=>["name"=>"william"]],
   "bruce"=>["name"=>"dodd","dog"=>["name"=>"bruce"]],
]

CollectionUtility::keyByProperty($items,["name","dog.name"])
[
    "john.william"=>["name"=>"john","dog"=>["name"=>"william"]],
    "frank.william"=>["name"=>"frank","dog"=>["name"=>"william"]],
    "dodd.bruce"=>["name"=>"dodd","dog"=>["name"=>"bruce"]],
]

CollectionUtility::groupByProperty($items,"dog.name")
[
    "william"=>[
        ["name"=>"john","dog"=>["name"=>"william"]],
        ["name"=>"frank","dog"=>["name"=>"william"]],
    ],
    "bruce"=>[
        ["name"=>"dodd","dog"=>["name"=>"bruce"]],
    ]
]

$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"with a prize at the bottom"
            ]
        ]
    ]
]

ArrayUtility::dotRead($array, "a.very.shallow.hole", "no prize!")
"no prize!"

ArrayUtility::dotRead($array, "a.very.deep.hole")
"with a prize at the bottom"

$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"with a prize at the bottom"
            ]
        ]
    ],
    "i"=>[
        "like"=>"candy"
    ]
]

$array = ArrayUtility::dotWrite($array, "a.very.deep.hole", "no prize!");
$array - ArrayUtility::dotWrite($array, "i.like", ["carrots","broccoli"]);

$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"no prize!"
            ]
        ]
    ],
    "i"=>[
        "like"=>["carrots","broccoli"]
    ]
]

$array = ArrayUtility::dotWrite($array, "a.very.shallow.hole", "that may contain a prize!");
$array = ArrayUtility::dotWrite($array, "i.also.like", "blue skies");

$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"no prize!"
            ]
        ]
    ],
    "a"=>[
        "very"=>[
            "shallow"=>[
                "hole"=>"that may contain a prize!"
            ]
        ]
    ],
    "i"=>[
        "like"=>["carrots","broccoli"]
        "also"=>[
            "like"=>"blue skies"
        ]
    ]
]

$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"with a prize at the bottom"
            ]
        ]
    ],
    "i"=>[
        "like"=>"candy"
    ]
]

ArrayUtility::dotMutate($array, "a.very.deep.hole", "no prize!");
ArrayUtility::dotMutate($array, "i.like", ["carrots","broccoli"]);

$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"no prize!"
            ]
        ]
    ],
    "i"=>[
        "like"=>["carrots","broccoli"]
    ]
]

ArrayUtility::dotMutate($array, "a.very.shallow.hole", "that may contain a prize!");
ArrayUtility::dotMutate($array, "i.also.like", "blue skies");

$array = [
    "a"=>[
        "very"=>[
            "deep"=>[
                "hole"=>"no prize!"
            ]
        ]
    ],
    "a"=>[
        "very"=>[
            "shallow"=>[
                "hole"=>"that may contain a prize!"
            ]
        ]
    ],
    "i"=>[
        "like"=>["carrots","broccoli"]
        "also"=>[
            "like"=>"blue skies"
        ]
    ]
]