Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional array to object, specific way

I know this type of question has been asked a lot, but I haven't found out how to do it like I want it to be.

So, basically I have this array.

array(7) {
  ["site"]=>
  array(5) {
    ["production"]=>
    bool(false)
    ["url"]=>
    string(29) "http://localhost/"
    ["name"]=>
    string(6) "Sitename"
    ["title"]=>
    string(7) ": Index"
    ["pagedata"]=>
    array(1) {
      ["default"]=>
      string(5) "Index"
    }
  }
  ["DB"]=>
  array(5) {
    ["host"]=>
    string(9) "localhost"
    ["user"]=>
    string(4) "root"
    ["pass"]=>
    string(4) "secret"
    ["database"]=>
    string(12) "database"
    ["engine"]=>
    string(7) "eMySQLi"
  }
  ["cache"]=>
  array(2) {
    ["file"]=>
    array(1) {
      ["time"]=>
      int(500)
    }
    ["memcache"]=>
    array(2) {
      ["my_first_vps_ip"]=>
      string(17) "my_first_vps_port"
      ["my_second_vps_ip"]=>
      string(18) "my_second_vps_port"
    }
  }
  ["skin"]=>
  array(2) {
    ["name"]=>
    string(9) "thehabbos"
    ["mobile"]=>
    array(2) {
      ["enabled"]=>
      bool(true)
      ["name"]=>
      string(16) "mobile_thehabbos"
    }
  }
  ["lang"]=>
  array(1) {
    ["name"]=>
    string(7) "English"
  }
  ["widget"]=>
  array(1) {
    ["default"]=>
    string(7) "Icecron"
  }
  ["cron"]=>
  array(1) {
    ["DatabaseBackup"]=>
    array(1) {
      ["execute_every"]=>
      int(86400)
    }
  }
}

So, if I 'parse' this array ( convert the array into an object ) with a method like this one..

    private function parse($arr)
    {
        foreach ($arr as $key => $val) 
        {
            $this->{$key} = is_array($val) ? $this->parse($val) : $val;
        }
        return $this;
    }

I'll end up with something like...

object(Configure)#3 (27) {
  ["production"]=>
  bool(false)
  ["url"]=>
  string(29) "http://localhost/RevFramework"
  ["name"]=>
  string(7) "English"
  ["title"]=>
  string(7) ": Index"
  ["default"]=>
  string(7) "Icecron"
  ["pagedata"]=>
  *RECURSION*
  ["site"]=>
  *RECURSION*
  ["host"]=>
  string(9) "localhost"
  ["user"]=>
  string(4) "root"
  ["pass"]=>
  string(4) "root"
  ["database"]=>
  string(12) "rev_database"
  ["engine"]=>
  string(7) "eMySQLi"
  ["DB"]=>
  *RECURSION*
  ["time"]=>
  int(500)
  ["file"]=>
  *RECURSION*
  ["my_first_vps_ip"]=>
  string(17) "my_first_vps_port"
  ["my_second_vps_ip"]=>
  string(18) "my_second_vps_port"
  ["memcache"]=>
  *RECURSION*
  ["cache"]=>
  *RECURSION*
  ["enabled"]=>
  bool(true)
  ["mobile"]=>
  *RECURSION*
  ["skin"]=>
  *RECURSION*
  ["lang"]=>
  *RECURSION*
  ["widget"]=>
  *RECURSION*
  ["execute_every"]=>
  int(86400)
  ["DatabaseBackup"]=>
  *RECURSION*
  ["cron"]=>
  *RECURSION*
}

So, like that I could use it like this..

echo $this->url;

But what I want to do, is use it like this..

echo $this->site->url;

Any idea on how I could accomplish this, if possible?

like image 937
Manuel Fernández Avatar asked Dec 22 '22 05:12

Manuel Fernández


2 Answers

You'll need to instantiate a new object for your array values to set properties on. Unless you have something more specific, a stdClass will do:

private function parse(array $arr, stdClass $parent = null) {
    if ($parent === null) {
        $parent = $this;
    }

    foreach ($arr as $key => $val) {
        if (is_array($val)) {
            $parent->$key = $this->parse($val, new stdClass);
        } else {
            $parent->$key = $val;
        }
    }

    return $parent;
}
like image 160
deceze Avatar answered Dec 24 '22 02:12

deceze


I think this will do what you want:

$obj = json_decode(json_encode($my_array));
echo $obj->site->url;
like image 24
Vikk Avatar answered Dec 24 '22 02:12

Vikk