Arr::build

Posted on November 14, 2016

Build a new array using a callback.

Example usage

 

$example_array = Arr::build(
    array('key1' => 'value1'),
    function($key, $value) {
        return array(
            (int) str_replace('key', '', $key),
            (int) str_replace('value', '', $value)
        );
    }
);

// [1 => 1]

$array1 = [
    ['id' => 1, 'name' => 'John Doe'],
    ['id' => 2, 'name' => 'Jane Doe'],
    ['id' => 3, 'name' => 'Jack Doe']
]; 

$array2 = Arr::build($array1, function($key, $value) {
    return [$value['id'], $value['name']];
});

// [
//     1 => 'John Doe',
//     2 => 'Jane Doe',
//     3 => 'Jack Doe'
// ]

Parameters

array $array

callable $callable

Return Value

Array with new array.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *