separate array laravel

0

I receive data from a view in this way, is there a way to separate them and make it like the example below?

    array:6 [▼
          "id_producto" => array:1 [▼
            "insumo" => "7"
          ]
          "precio" => array:1 [▼
            "precio" => "4500"
          ]
          "cantidad" => array:1 [▼
            "cantidad" => "1"
          ]
          "total" => array:1 [▼
            "total" => "4500"
          ]
          "estado" => 1
          "id_control" => 58
        ]

expected result,

   array:6 [▼
          "id_producto" =>  "7"
          ]
          "precio" =>  "4500"
          ]
          "cantidad" =>  "1"
          ]
          "total" =>  "4500"
          ]
          "estado" => 1
          "id_control" => 58
        ]

This is just part of the problem, I narrow it down to be shorter, thank you very much in advance

    
asked by Renato 01.12.2017 в 19:05
source

1 answer

3

As you have it, the simplest solution would be:

<?php

$miarray=["id_producto" =>[
            "insumo" => "7"
          ],
          "precio" =>  [
            "precio" => "4500"
          ],
          "cantidad" => [
            "cantidad" => "1"
          ],
          "total" => [
            "total" => "4500"
          ],
          "estado" => 1,
          "id_control" => 58
        ];

$array_parseado = [];

foreach($miarray as $key => $value ) {
    $array_parseado[$key]=is_array($value) ? array_values($value)[0]:$value;    
}

var_dump($array_parseado);

But with that I'm assuming that the array you want to parsed has its nested values at most at a depth level, among other things.

    
answered by 01.12.2017 / 19:23
source