Created
October 13, 2015 16:39
-
-
Save oksuz/0c40d9fad5f0413b5059 to your computer and use it in GitHub Desktop.
dot notation to array in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class test | |
{ | |
protected $data; | |
public function set($key, $value) | |
{ | |
$points = explode(".", $key); | |
$tmp =& $this->data; | |
foreach ($points as $p) { | |
if (!isset($tmp[$p])){ | |
$tmp[$p] = []; | |
} | |
$tmp =& $tmp[$p]; | |
} | |
$tmp = $value; | |
} | |
public function get($key) | |
{ | |
$tmp =& $this->data; | |
$points = explode(".", $key); | |
foreach ($points as $p) { | |
if (isset($tmp[$p])) { | |
$tmp =& $tmp[$p]; | |
} | |
} | |
return $tmp; | |
} | |
public function getData() | |
{ | |
return $this->data; | |
} | |
} | |
$t = new test; | |
$t->set("user.name", "yunus"); | |
$t->set("user.surname", "oksuz"); | |
$t->set("user.school.name", "SDU"); | |
var_dump($t->get("user")); | |
var_dump($t->get("user.school")); | |
// | |
var_dump($t->getData()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment