1

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"
    }
  }
}

2
  • So? What is your question? Also, test driven development, just a suggestion. Commented Dec 4, 2020 at 21:38
  • @UlrichEckhardt that function is only doing a 1 dimensional array, so the key pagination.el is being missed. I need to make it multi dimensional Commented Dec 4, 2020 at 21:40

1 Answer 1

3

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
                                )

                        )

                )

        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I did do that exact same thing but it didn't work, I can see you modified other parts of the function though which is probably why it didn't work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.