-3

I have a PHP class as follows:

Class ArrayStore{

static $storage = array();

public static set($key, $value){
  //set value
}

public static get($key){
  //return value
}

}

How I want to use:

ArrayStore::set('["id"]["name"]["last"]', 'php');
ArrayStore::get('["id"]["name"]["last"]'); //should return php

ArrayStore::set('["multi"]["array"]', 'works');
ArrayStore::get('["multi"]["array"]'); //should return works

Let me know if there is a better way of setting and getting a multidimensional array with reason.

Edit: I tried something like this:

<?php
$x = array(1=>'a');
$op="\$x"."[1]";
$value=eval("return ($op);");

echo $value;//prints a.
?>
Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34
  • The code you posted is not OOP but procedural programming (with global variables) under disguise. PHP already provides the [`ArrayObject`](http://php.net/manual/en/class.arrayobject.php) class that works as an array. Use it! – axiac Jun 14 '17 at 11:46

1 Answers1

0

From your logic you can do like this:

<?php
class ArrayStore{

static $storage = array();

public static function  set($key, $value){
  self::$storage[$key] = $value;
}

public static function  get($key){
  return self::$storage[$key];
}

}
ArrayStore::set('["id"]["name"]["last"]', 'php');
echo ArrayStore::get('["id"]["name"]["last"]'); //should return php
echo "<br>";
ArrayStore::set('["multi"]["array"]', 'works');
echo ArrayStore::get('["multi"]["array"]'); //should return works
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • It returns the expected output. However, It is not storing the data like `array('id' => array('name'=>array('last' => 'php')))` – Sahith Vibudhi Jun 14 '17 at 11:49