Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:21:23+00:00 2026-05-27T03:21:23+00:00

I am trying to write a shoping cart in php and I have a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:21:23+00:00Added an answer on May 27, 2026 at 3:21 am

    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";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to write something to get my images to show correctly. I have
I'm trying write a query to find records which don't have a matching record
I have a problem when I create page with the script showing document.write(Text); text
I'm trying to write a simple shopping cart Orchard module that will store items
I'm trying to write a macro and I have the 2nd half done (tile
i have been trying to write some programs on java sockets and i got
I am trying to write a dialog box in my app . The problem
I'm trying to write to a file index.html that I have in my resources.
So I have these 2 tables and I'm trying to write the most efficient
I am new to programming and trying to write a javascript function to set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.