Laravel create a multidimensional array

0

I have a query in mysql that returns a row of records like this, the first field is the marca and the second the modelo , what I try to do with PHP is to mount an array with the following structure to pass it in view of blade grouped by brand and vehicle.

Sample SQL results

'FORD','FOCUS',1,2,3

'FORD','FOCUS',2,2,3

'FORD','FOCUS',3,1,2

'FORD','TRANSIT',1,2,3

'FORD','TRANSIT',2,2,3

'FORD','GLOBO',1,2,3

The array I want to mount is type this

$coches = [
    'ford' => [
        focus =>  [
            valor => 1,
            valor => 2,
        ]
        TRANSIT =>  [
            valor => 1,
            valor => 2,
        ]
    ]

]

On the other hand, the array that I have if I do a "dump" is kind of this

array:1 [▼
  0 => array:1 [▼
    "FORD" => {#205 ▼
      +"code": "FOCUS"
      +"title": "TEST"
    }
  ]
]

The code that I have in PHP is something like this

$sql = "SELECT xxx ... ...";

$results = DB::select( DB::raw($sql), ['id' => $id]);

$newCar = array();
$newCars = array();

foreach ($results as $cars => $car)
{
    if (empty($newCars)) {
        $newCar[$course->code] = $course;
        array_push($newCourses,$newCourse);
    }


    if (!array_key_exists('FORD',$newCourses)){
        echo '-> '. $course->code.'<br>';
    }

}

The last if , does not fit anymore.

The logic that I try to do is create an array by car and model to pass it to the orderly view.

    
asked by ilernet 01.08.2018 в 15:01
source

1 answer

1

As an initial observation, you will not be able to have an arrangement with the following form:

$coches = [
    'ford' => [
        'focus' =>  [
            'valor' => 1,
            'valor' => 2,
        ]
        'transit' =>  [
            'valor' => 1,
            'valor' => 2,
        ]
    ]
]

Well, if you look inside focus you are creating entries with the same key / key: valor . What you can do is the following:

$coches = [
    'ford' => [
        'focus' =>  [
            'valor_1' => 1,
            'valor_2' => 2,
        ]
        'transit' =>  [
            'valor_1' => 1,
            'valor_2' => 2,
        ]
    ]
]

To achieve this you can do it in several ways, one is with the library Collection of Laravel. In fact, any questions with Eloquent or QueryBuilder returns an instance of this class.

PD: Since it does not match the way you put your results with what was used in your mapping method, I will assume that the records come to you in an arrangement as follows:

[
    ['FORD','FOCUS',1,2,3],
    ['FORD','FOCUS',1,2,3],
    ['FORD','FOCUS',1,2,3],
    ...
]

Solution

Then you can do the following:

// Creamos una instancia de Collection con los resultados
$results = collect(DB::select( DB::raw($sql), ['id' => $id]));

$coches = $results
    // Mapeamos el arreglo para que tenga llaves útiles
    ->mapWithKeys(function ($item) {
        return [
            'brand'   => $item[0],
            'type'    => $item[1],
            'valor_1' => $item[2],
            'valor_2' => $item[3],
            'valor_3' => $item[4],
        ];
    })
    // Realizamos la agrupación de elementos
    ->groupBy(['brand', 'type']);

This should give you an arrangement in the following way:

$coches = [
    'ford' => [
        'focus'   => [
            [
                'brand'   => 'ford',
                'type'    => 'focus',
                'valor_1' => 1,
                'valor_2' => 2,
                'valor_3' => 3,
            ]
        ],
        'transit' => [
            [
                'brand'   => 'ford',
                'type'    => 'transit',
                'valor_1' => 1,
                'valor_2' => 2,
                'valor_3' => 3,
            ]
        ]
    ]
];

You can use other Collection methods to make it exactly what you want, such as Map () with which you could delete the unnecessary keys ( brand , type ?), but you can already see what it is to play with the methods to achieve the desired result.

    
answered by 01.08.2018 в 17:11