Skip to content

Instantly share code, notes, and snippets.

@oksuz
Created October 13, 2015 16:39
Show Gist options
  • Save oksuz/0c40d9fad5f0413b5059 to your computer and use it in GitHub Desktop.
Save oksuz/0c40d9fad5f0413b5059 to your computer and use it in GitHub Desktop.
dot notation to array in php
<?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