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

Asked

Viewed 436 times

1

I usually use the function array_map to be able to do something similar to the "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 closely at the Manual, I realized that after the first parameter the number of arrays past can be infinite:

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

What is the purpose of this?

  • What are the arrays ?

  • I’m still asking myself - "What do you want to know for sure" : http://stackoverflow.com/questions/3432257/difference-between-array-map-array-walk-and-array-filter

  • I think by showing the traditional usage difference and wondering why this function accepts N arrays parameters I’ve made it clear enough.

  • Not that I want to appear arrogant or ignorant, but on the very page of PHP.net, the examples are more than explicit, and explain the function and the infinite arguments it can receive. And besides, your question is also the answer you seek, in my view clear ! Maybe I’m misunderstanding, but that’s why I just showed that difference. Take a look at the image before example #4 : http://php.net/manual/en/function.array-map.php this might help with the question. And please don’t misunderstand me, I have the best intentions.

  • 2

    @Edilson, it is, manual in English. It seems to me that (almost always) the Manual in Portuguese comes missing something. And the Handbook doesn’t always serve to teach you anything that works in practice, or even say for sure How a function works. So I prefer to ask here :p

  • Ah, how could I forget that -, I almost never read the manual in Portuguese, now for sure I was ignorant, I did not evaluate the conditions before. However I think the @Ivan Ferrer answer will help you.

Show 1 more comment

3 answers

2

I believe this is because when you create the call back function you can use infinite parameters so the array_map allows as many parameters as possible, as long as it is compatible with your callback function.

2


Within the array_map(), the method callback can perform a mapping with the 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);

That would be the way out:

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
                )

        )

)

Take this example: http://www.viper-7.com/H9Lx6f

  • 1

    +1 Correct, Ivan Ferrer. It would be good to add examples to the answer, because the links will one day be broken

0

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

A small example, where we have three arrays, that will be merged, to the taste 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 incredible comeback 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',
  ),
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.