IT/php

php array_map 배열 첨자 합치기

조원태 2016. 10. 6. 14:24
반응형

php array_map 배열 첨자 합치기

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

array_map — Applies the callback to the elements of the given arrays



array array_map ( callable $callback , array $array1 [, array $... ] )



<?php

function show_Spanish($n, $m)

{

    return("The number $n is called $m in Spanish");

}


function map_Spanish($n, $m)

{

    return(array($n => $m));

}


$a = array(1, 2, 3, 4, 5);

$b = array("uno", "dos", "tres", "cuatro", "cinco");


$c = array_map("show_Spanish", $a, $b);

print_r($c);


$d = array_map("map_Spanish", $a , $b);

print_r($d);

?>


// printout of $c

Array

(

    [0] => The number 1 is called uno in Spanish

    [1] => The number 2 is called dos in Spanish

    [2] => The number 3 is called tres in Spanish

    [3] => The number 4 is called cuatro in Spanish

    [4] => The number 5 is called cinco in Spanish

)


// printout of $d

Array

(

    [0] => Array

        (

            [1] => uno

        )


    [1] => Array

        (

            [2] => dos

        )


    [2] => Array

        (

            [3] => tres

        )


    [3] => Array

        (

            [4] => cuatro

        )


    [4] => Array

        (

            [5] => cinco

        )


)

반응형