0

I'm currently attempting to create a dot notation multidimensional array except when the array is sequential instead of associative, so for example:

Array
(
    [cars] => Array 
        (
           [specs] => Array
               (
                   [0] => Array
                       (
                          [brand] => BMW
                          [model] => Series 3
                       )
                   [1] => Array
                       (
                          [brand] => Mercedes
                          [model] => CLA
                       )
               )
           [size] => "Sedan"
        ) 
)

In the example above I expect to get

Array
(
    [cars.specs] => Array 
        (
            [0] => Array
                 (
                    [brand] => BMW
                    [model] => Series 3
                 )
            [1] => Array
                 (
                    [brand] => Mercedes
                    [model] => CLA
                 )
            )
    [cars.size] => "Sedan"
)

So im expecting to have associative arrays with dotted notation but sequential arrays should remain undoted.

I've tried doing

use Illuminate\Support\Arr;

function dottedArray($array, &$finalArray = array()){
        if(!is_array($array)){
            return;
        }

        foreach($array as $key => $value){
            $finalArray[$key] = $value;
            if(is_array($value)){
                 if(!(Arr::isAssoc($value))){
                     $finalArray[$key] = Arr::undot($finalArray [$key]);
                 }
                 else{
                     $finalArray[$key] = Arr::dot($finalArray[$key]);
                     dottedArray($value, $finalArray[$key]);
                 }
            }
        }

   return $finalArray;
}

dottedArray($array);

I was trying to use laravel helper functions to dot and undot the array, but I'm not sure this is the right way of doing it

0 Answers0