Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

functions to get/set values in multidimensional arrays dynamically

I am trying to write a shoping cart in php and I have a problem with get/set values in multidimentional arrays. I keep the current order in $_SESSION['basket']. It looks like that:

[basket] => Array
        (
            [0] => Array
                (
                    [pid] => 3
                    [name] => Camera
                    [price] => 200.99
                    [quantity] => 1

                )

            [1] => Array
                (
                    [pid] => 5
                    [name] => Computer
                    [price] => 320.99
                    [quantity] => 1

                    [extras] => Array
                        (
                            [0] => Array
                                (
                                    [pid] => 86
                                    [name] => RAM
                                    [price] => 99
                                    [qty] => 1
                                )

                            [1] => Array
                                (
                                    [pid] => 98
                                    [name] => CD-ROM
                                    [price] => 19.99
                                    [qty] => 1
                                )

                        )

                )
 )

Every item is stored as a subarray. I have a function, which checks if a given item exists in the basket array and returns the path to it. For example, if I want to check for a product with id 98 (CD-Rom), the function returns the following path: 1:extras:1.

I cant figure out how to use the path if I want to get or a set a value in the array. Is it possible to construct the path to an array key, without the use of eval()? I have these functions:

 function get_val($array, $path, $key) {
    //some code
    return eval('return '.$array.$path.$key.';');
 }

So, $price = get_val($_SESSION['basket'], $path, 'price'); should return the price for CD-ROM (19.99)

 function set_val($array, $path, $key, $value) {
    //some code
    $str =  eval(''.$array.$path.$key.';');
    $str = $value;
 }

set_val($_SESSION['basket'], $path, 'price', '30'); will set the price for CD-ROM to 30.

Is there a better way for doing this?

Thank you.

like image 299
casper Avatar asked Nov 23 '11 14:11

casper


People also ask

How do you find the value of multidimensional array?

Size of multidimensional arrays: The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.

Which function is used to created dynamic 2D arrays?

The malloc() function is used in c programming to store the data in the heap which is dynamic memory storage. It is mostly used for the dynamic declaration of the arrays and also be used for the creation of two-dimensional arrays.

How do I pass a 2D dynamic array to a function in CPP?

int **board; board = new int*[boardsize]; for (int i = 0; i < boardsize; i++) board[i] = new int[size]; You need to allocate the second depth of array. Save this answer.


1 Answers

Here you go a code I have finetuned some time ago:

  function get_val($array,$path) {
    for($i=$array; $key=array_shift($path); $i=$i[$key]) {
      if(!isset($i[$key])) return null;
    }
    return $i;
  }

  function set_val(&$array,$path,$val) {
    for($i=&$array; $key=array_shift($path); $i=&$i[$key]) {
      if(!isset($i[$key])) $i[$key] = array();
    }
    $i = $val;
  }

See this test example, I believe it is what you are looking for:

  $data = array("x"=>array("y"=>array("z"=>"foo")));
  echo get_val($data,array("x","y","z")); // foo
  set_val($data,array("x","y","u"),"bar"); // $data["x"]["y"]["u"] = "bar";
like image 193
Jan Turoň Avatar answered Oct 11 '22 22:10

Jan Turoň