What is the purpose of array_map to accept infinite array parameters after callback?

1

I usually use the array_map function to be able to do something similar to% list comprehension of python

Something like:

$meses = function($mes)
{
  return sprintf('02%s/%s', $mes, date('Y'));
}

$arrayFormatado = array_map($meses, range(1, 12));

// Retorna: array(){ '01/2015', '02/2015', ...}

But now looking at the Manual, I realized that after the first parameter the number of arrays passed can be infinite:

array array_map ( callback $callback , array $arr1 [, array $... ] )

What is the purpose of this?

    
asked by anonymous 22.10.2015 / 15:57

3 answers

2

Within array_map() , the method callback can map to other items in the list.

<?php

   $array1 = array('vendedores', 'estudante',     'colaborador');
   $array2 = array('balconista', 'estagiário',    'operário');
   $array3 = array('entregador', 'bibliotecário', 'programador');

   $array_group = array('lista1', 'lista2', 'lista3');

   function mapDadosToCategory($grupos, $array1, $array2, $array3) {

        return array($grupos => array($array1, $array2, $array3));
   }


   $map = array_map('mapDadosToCategory', $array_group, $array1, $array2, $array3); //n arrays mapeados

   echo '<pre>';
    print_r($map);

The output would be this:

Array
(
    [0] => Array
        (
            [lista1] => Array
                (
                    [0] => vendedores
                    [1] => balconista
                    [2] => entregador
                )

        )

    [1] => Array
        (
            [lista2] => Array
                (
                    [0] => estudante
                    [1] => estagiário
                    [2] => bibliotecário
                )

        )

    [2] => Array
        (
            [lista3] => Array
                (
                    [0] => colaborador
                    [1] => operário
                    [2] => programador
                )

        )

)

See this example: link

    
22.10.2015 / 18:52
2

I think this is because when you create it creates the call back function you can use infinite parameters as soon as the array_map allows as many parameters as it is compatible with your callback function.

    
22.10.2015 / 17:51
0

The purpose is to map all arrays passed at the same time. This can be useful when you need to apply a "merge" on certain arrays, so you could better control the output.

A small example, where we have three arrays , which will be merged, to the liking of the programmer:)

$a = array('one', 'two', 'tree');

$b = array('uno', 'dos', 'tres', 'um passito pra frente maria');

$c = array('um', 'dois', 'três', 'nove');


$all = array_map(function ($a, $b, $c)
{
    return ['a' => $a, 'b' => $b, 'c' => $c];

}, $a, $b, $c);


var_export($all);

The return will be:

array (
  0 => 
  array (
    'a' => 'one',
    'b' => 'uno',
    'c' => 'um',
  ),
  1 => 
  array (
    'a' => 'two',
    'b' => 'dos',
    'c' => 'dois',
  ),
  2 => 
  array (
    'a' => 'tree',
    'b' => 'tres',
    'c' => 'três',
  ),
  3 => 
  array (
    'a' => NULL,
    'b' => 'um passito pra frente maria',
    'c' => 'nove',
  ),
)
    
22.10.2015 / 19:21