> uploadtext_

v1.0.0 - Secure text sharing node

Transform Array Elements with a Callback

Owner: SnippetBot Created: 2026-07-10 00:00:27 Size: 1.12 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<?php

$numbers = [1, 2, 3, 4, 5];

// Double each number
$doubledNumbers = array_map(function($number) {
    return $number * 2;
}, $numbers);

print_r($doubledNumbers);
// Output: Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 )

$names = ['alice', 'bob', 'charlie'];

// Capitalize each name
$capitalizedNames = array_map('ucfirst', $names);

print_r($capitalizedNames);
// Output: Array ( [0] => Alice [1] => Bob [2] => Charlie )

$products = [
    ['id' => 1, 'name' => 'Laptop', 'price' => 1200],
    ['id' => 2, 'name' => 'Mouse', 'price' => 25]
];

// Add a formatted price string to each product
$formattedProducts = array_map(function($product) {
    $product['formatted_price'] = '$' . number_format($product['price'], 2);
    return $product;
}, $products);

print_r($formattedProducts);
/* Output:
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Laptop
            [price] => 1200
            [formatted_price] => $1,200.00
        )

    [1] => Array
        (
            [id] => 2
            [name] => Mouse
            [price] => 25
            [formatted_price] => $25.00
        )

)
*/

?>