Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

8,367 questions

11,628 answers

573 users

How to apply callback function to each element of an array in PHP

3 Answers

0 votes
function power2($n)
{
    return $n * $n;
}

$arr = array(1, 2, 3, 4, 5, 6, 7);
$arr_power2 = array_map("power2", $arr);

print_r($arr_power2);

/*
run:
 
Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 [5] => 36 [6] => 49 ) 
 
*/

 




answered Jul 27, 2015 by avibootz
0 votes
function power3($n)
{
    return $n * $n * $n;
}

$arr = array(1, 2, 3, 4, 5, 6, 7);
$arr_power3 = array_map("power3", $arr);

print_r($arr_power3);

/*
run:
 
Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 [5] => 216 [6] => 343 ) 
 
*/

 




answered Jul 27, 2015 by avibootz
0 votes
$numbers = array(1, 2, 3, 4);
$words = array("one", "two", "three", "four");
$numbers_words = array_map(null, $numbers, $words);

echo "<pre>";
print_r($numbers_words);
echo "</pre>";

/*
run:
 
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => one
        )

    [1] => Array
        (
            [0] => 2
            [1] => two
        )

    [2] => Array
        (
            [0] => 3
            [1] => three
        )

    [3] => Array
        (
            [0] => 4
            [1] => four
        )

)
 
*/

 




answered Jul 27, 2015 by avibootz
...