Martyn Ball
Martyn Ball

Reputation: 4885

Making this function recursive on an array

I have got the following array:

{
  "slider": {
    "effect": {
      "el": ".custom-class"
    },
    "custm": "slider",
    "arrowsEnabled": "1",
    "pagination.el": ".swiper-pagination"
  }
}

Any key which has got the dot notation I need use this function on:

function convertDotToArray($array)
{
    $newArray = array();
    foreach ($array as $key => $value) {
        $dots = explode(".", $key);
        if (count($dots) > 1) {
            $last = &$newArray[ $dots[0] ];
            foreach ($dots as $k => $dot) {
                if ($k === 0) {
                    continue;
                }
                $last = &$last[$dot];
            }

            $last = $value;
        } else {
            $newArray[$key] = $value;
        }
    }

    return $newArray;
}

However due to the only key I have got being 2 level deep that function isn't applying to the pagination.el key. I need make this function recursively go over any array passed into it so that no matter how deep the keys are they are converted, so my array should turn into this:

{
  "slider": {
    "effect": {
      "el": ".custom-class"
    },
    "custm": "slider",
    "arrowsEnabled": "1",
    "pagination": {
      "el": ".swiper-pagination"
    }
  }
}

Upvotes: 1

Views: 58

Answers (1)

Kerkouch
Kerkouch

Reputation: 1456

You will need to use recursivity:

function convertDotToArray($array)
{
    $newArray = [];

    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $value = convertDotToArray($value);
        }

        $parts = explode('.', $key);
        $pointer = &$newArray;

        foreach ($parts as $part) {
            $pointer = &$pointer[$part];
        }

        $pointer = $value;
    }

    return $newArray;
}

Example:

<?php
$jsonData = <<<EOD
{
  "slider": {
    "effect": {
      "el": ".custom-class"
    },
    "custm": "slider",
    "arrowsEnabled": "1",
    "pagination.el.el.el": ".swiper-pagination",
    "deep.deep":{"deep.deep":"deep"}
  }
}
EOD;

$data = json_decode($jsonData, true);
print_r(convertDotToArray($data));

gives the following output:

Array
(
    [slider] => Array
        (
            [effect] => Array
                (
                    [el] => .custom-class
                )

            [custm] => slider
            [arrowsEnabled] => 1
            [pagination] => Array
                (
                    [el] => Array
                        (
                            [el] => Array
                                (
                                    [el] => .swiper-pagination
                                )

                        )

                )

            [deep] => Array
                (
                    [deep] => Array
                        (
                            [deep] => Array
                                (
                                    [deep] => deep
                                )

                        )

                )

        )

)

Upvotes: 3

Related Questions