How to combine an associative array with another associative array in Laravel? Problem with collections

1

I am testing the collections, and I have 2 doubts, the first is when I have arrived at the method where I am having problems when using LIKE. Given this collection

    $coleccion = collection(  [
                        ['vehiculo' => 'toyota','propietario' => 'akio'],
                        ['vehiculo' => 'opel','propietario' => 'rosari'],
                        ['vehiculo' => 'mitsubishi','propietario' => 'ramon'],
                        ['vehiculo' => 'reanult','propietario' => 'velvet'],
                        ['vehiculo' => 'citroen','propietario' => 'anarella'],
                        ['vehiculo' => 'mercedes','propietario' => 'juana'],
                        ['vehiculo' => ['cadillac','toyota'],'propietario' => 'sandro'],
                  ]);

 $resultado = $coleccion->where('vehiculo','LIKE','"%opel%"');

dd($resultado)

The result variable I have tried it in several ways and in none of them I get any results

$resultado = $coleccion->where('vehiculo','LIKE',"%opel%");

 $resultado = $coleccion->where('vehiculo','LIKE','%opel%');

 $resultado = $coleccion->where('vehiculo','LIKE','"%op%"');

 $resultado = $coleccion->where('vehiculo','like','"%opel%"');

 $resultado = $coleccion->where('vehiculo','"%opel%"');

If I use this using a model, it works for me but not in this way.

My second doubt is the one that opens this post, I would like to combine in each of the arrays another key with another value, I have tried using combine, and merge and something I am doing wrong that does not combine it, simply add it to me at the end as a key more.

Starting from the same collection,

$coleccion = collection(  [
                            ['vehiculo' => 'toyota','propietario' => 'akio'],
                            ['vehiculo' => 'opel','propietario' => 'rosari'],
                            ['vehiculo' => 'mitsubishi','propietario' => 'ramon'],
                            ['vehiculo' => 'reanult','propietario' => 'velvet'],
                            ['vehiculo' => 'citroen','propietario' => 'anarella'],
                            ['vehiculo' => 'mercedes','propietario' => 'juana'],
                            ['vehiculo' => ['cadillac','toyota'],'propietario' => 'sandro'],
                      ]);

I've tried like this

$combinados = $coleccion->merge([0 => ['cilindraje' => 100],1 => ['cilindraje' => 1000],2 => ['cilindraje' => 200], 3 => ['cilindraje' => 350],4 => ['cilindraje' => 488],5 => ['cilindraje' => 665],6 => ['cilindraje' => 147]]);

and so

$combinados = $coleccion->merge( ['cilindraje' => 100], ['cilindraje' => 1000],['cilindraje' => 200],  ['cilindraje' => 350], ['cilindraje' => 488], ['cilindraje' => 665],['cilindraje' => 147]]);

and it does not work for me. As I say the final result should be this

 $coleccion = collection(  [
                            ['vehiculo' => 'toyota','propietario' => 'akio','cilindraje' => 100],
                            ['vehiculo' => 'opel','propietario' => 'rosari',cilindraje' => 1000],
                            ['vehiculo' => 'mitsubishi','propietario' => 'ramon',cilindraje' => 200],
                            ['vehiculo' => 'reanult','propietario' => 'velvet',cilindraje' => 350],
                            ['vehiculo' => 'citroen','propietario' => 'anarella',cilindraje' => 488],
                            ['vehiculo' => 'mercedes','propietario' => 'juana',cilindraje' => 665],
                            ['vehiculo' => ['cadillac','toyota'],'propietario' => 'sandro',cilindraje' => 147],
                      ]);
    
asked by KurodoAkabane 25.05.2017 в 10:47
source

2 answers

1

Look for the first thing you can do to use the filter, it would be something like this:

$coleccion = collect(  [
                    ['vehiculo' => 'toyota','propietario' => 'akio'],
                    ['vehiculo' => 'opel','propietario' => 'rosari'],
                    ['vehiculo' => 'mitsubishi','propietario' => 'ramon'],
                    ['vehiculo' => 'reanult','propietario' => 'velvet'],
                    ['vehiculo' => 'citroen','propietario' => 'anarella'],
                    ['vehiculo' => 'mercedes','propietario' => 'juana'],
                    ['vehiculo' => ['cadillac','toyota'],'propietario' => 'sandro'],
              ]);

    $resultado = $coleccion->filter(function($value,$key){

        $vehiculo = $value["vehiculo"];

        if(is_array($vehiculo)){
            $resultado = collect($vehiculo)->filter(function($value1,$key1){
                return str_contains($value1,"opel");
            });
            return $resultado->isNotEmpty();
        } else {
            return str_contains($vehiculo,"opel");
        }
    });

And for the second, you can use the map, it would be like this:

$coleccion = collect(  [
                        ['vehiculo' => 'toyota','propietario' => 'akio'],
                        ['vehiculo' => 'opel','propietario' => 'rosari'],
                        ['vehiculo' => 'mitsubishi','propietario' => 'ramon'],
                        ['vehiculo' => 'reanult','propietario' => 'velvet'],
                        ['vehiculo' => 'citroen','propietario' => 'anarella'],
                        ['vehiculo' => 'mercedes','propietario' => 'juana'],
                        ['vehiculo' => ['cadillac','toyota'],'propietario' => 'sandro'],
                  ]);

    $coleccion2 = collect([['cilindraje' => 100], ['cilindraje' => 1000],['cilindraje' => 200],  ['cilindraje' => 350], ['cilindraje' => 488], ['cilindraje' => 665],['cilindraje' => 147]]);

    $resultado = $coleccion->map(function($value,$key) use ($coleccion2) {
        return collect($value)->merge($coleccion2[$key])->toArray();
    });
    
answered by 27.05.2017 / 17:50
source
1

Instead of using merge to dry you should use array_merge , I'll attach an example to try it:

$array1 = array("id1" => "value1");

$array2 = array("id2" => "value2", "id3" => "value3", "id4" => "value4");

$array3 = array_merge($array1, $array2/*, $arrayN, $arrayN*/);
$array4 = $array1 + $array2;

echo '<pre>';
var_dump($array3);
var_dump($array4);
echo '</pre>';
    
answered by 25.05.2017 в 11:50