Skip to content

Instantly share code, notes, and snippets.

@wgrafael
Last active July 6, 2023 18:04
Show Gist options
  • Select an option

  • Save wgrafael/8a9bb1a963042bc88dac to your computer and use it in GitHub Desktop.

Select an option

Save wgrafael/8a9bb1a963042bc88dac to your computer and use it in GitHub Desktop.

Revisions

  1. wgrafael revised this gist Jan 24, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,8 @@
    * Convert array with key in dot notation for multidimensional array
    *
    * Example:
    * Input: [ "name.firstname" => "Rafael", "name.lastname" => "Dantas" ]
    * Output: [ "name" => [ "firstname" => "Rafael", "lastname" => "Dantas" ] ]
    * Input: [ "name.firstname" => "Rafael", "name.lastname" => "Dantas", "a.b.c" => "d" ]
    * Output: [ "name" => [ "firstname" => "Rafael", "lastname" => "Dantas" ], "a" => [ "b" => [ "c" => "d" ] ]
    *
    * @param $array Array with key in dot notation
    * @return Array array multidimensional
  2. wgrafael revised this gist Jan 24, 2017. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,14 @@
    <?php

    /**
    * Convert dot notation for multidimensional array
    * Convert array with key in dot notation for multidimensional array
    *
    * Example:
    * Input: [ "name.firstname" => "Rafael", "name.lastname" => "Dantas" ]
    * Output: [ "name" => [ "firstname" => "Rafael", "lastname" => "Dantas" ] ]
    *
    * @param $array Array with key in dot notation
    * @return Array array multidimensional
    * @author Rafael Dantas
    */
    function convertDotToArray($array) {
  3. wgrafael revised this gist Jan 7, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    /**
    * Convert dot notation for multidimensional array
    *
  4. wgrafael renamed this gist Jan 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. wgrafael created this gist Jan 7, 2016.
    24 changes: 24 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    /**
    * Convert dot notation for multidimensional array
    *
    * @author Rafael Dantas
    */
    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;
    }