FlatMap array_map (): Argument # 2 should be an array

1

I'm trying out some laravel helpers and I realize that either I'm doing something wrong or this is not working, I use laravel 5.2

This is my code.

    public function coleccionArrayAlfanumerico()
    {
      $resultado = collect(['pepino','porche','ramon','camisa','japones','movil','aprende el arte del pnl','Pentium IV','pescaderia']);

      return $resultado;
    }

    public function coleccionArrayClaves()
    {
        $clave = collect(['fruta', 'coche', 'nombre','ropa', 'idioma', 'tecnologia','libro', 'ORDENADOR', 'establecimiento']);

        return $clave;
    }

 public function flatMap()
       {

         $claves = $this->coleccionArrayClaves();
         $valores = $this->coleccionArrayAlfanumerico();
         $combinados = $claves->combine($valores);

         $flatmappeado = $combinados->flatMap(function($values){
            return array_map('strtoupper',$values);
          });

         $resultado = $flatmappeado->all();
         dd($resultado);
       }

The error is:

array_map(): Argument #2 should be an array

I do not understand very well why, I have based on the example of the documentation and I have created arrays manually so as not to have to take data from the database to avoid major problems.

Any ideas?

------------------------------ EDITED ---------------- ------------

The result of "combined" is

Collection {#252 ▼
  #items: array:9 [▼
    "fruta" => "pepino"
    "coche" => "porche"
    "nombre" => "ramon"
    "ropa" => "camisa"
    "idioma" => "japones"
    "tecnologia" => "movil"
    "libro" => "aprende el arte del pnl"
    "ORDENADOR" => "Pentium IV"
    "establecimiento" => "pescaderia"
  ]
}

Doing a dd ($ values) or a var_dump here

$flatmappeado = $combinados->flatMap(function($values){
          dd($values);
            return array_map('strtoupper',$values);
          });

Its result is "cucumber", that is, the first element of the array. Putting "$ values" or putting "$ values" the result is the same, that is "cucumber".

Shaz, it really draws attention that if you seem to be calling flatmap inside yourself, but I'm really catching the same example of documentation, just like that, that's why I'm surprised it does not work.

    
asked by KurodoAkabane 11.05.2017 в 12:39
source

1 answer

0
  • The reason you get the error is because you are giving a string of text to the array_map function as the second argument, array_map expects an array as the second argument. (figure 1)

  • The reason why $ values is equal to the string 'cucumber' is because the variable $ is an array of strings and its first value is 'cucumber'.

  • Conclusion: To avoid the error you should:

    • a) make sure that $ combined is an array of arrays
    • b) apply the strtoupper function directly to the variable $ values (figure 2). Which would return as a result an array exactly equal to $ combined.

        
    answered by 03.06.2017 в 02:53