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 6652167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:06:09+00:00 2026-05-26T01:06:09+00:00

I am trying to implement multidimensional array that hold data of Pizza id’s with

  • 0

I am trying to implement multidimensional array that hold data of Pizza id’s with options and extras id’s.

Let look at the following scenario…

Customer wants

  • Two ‘Chicken Pizza’ (ProductID:12) – ’10 inches’ (OptionsID:4) with extras of Ham and Tuna (ExtrasID: 5,10)

  • One ‘Chicken Pizza’ (ProductID:12) – ’10 inches’ (OptionsID:4) with extras of Sweet Corn (ExtrasID: 2)

  • One ‘Chicken Pizza’ (ProductID:12) – ’14 inches’ (OptionsID:2) with no extras

  • Eleven ‘Vegetarian Pizza’ (ProductID:35) – ‘7 inches’ (OptionsID:52) with no extras

See the following code below that match the scenario… Im I doing it right? or what can be done to improve it and readable?

//Two 'Chicken Pizza' (ProductID:12) - '10 inches' (OptionsID:4) 
//With extras of Ham and Tuna (ExtrasID: 5,10)
$option4[] = array(
    'quantity' => 2,
    'extras_id' => array(5, 10)
);

//One 'Chicken Pizza' (ProductID:12) - '10 inches' (OptionsID:4) 
//With extras of Sweet Corn (ExtrasID: 2)
$option4[] = array(
    'quantity' => 1,
    'extras_id' => array(2)
);

//One 'Chicken Pizza' (ProductID:12) - '14 inches' (OptionsID:2) 
//With no extras
$option2[] = array(
    'quantity' => 1,
    'extras_id' => array()
);

//Eleven 'Vegetarian Pizza' (ProductID:35) - '7 inches' (OptionsID:52) 
//With no extras
$option52[] = array(
    'quantity' => 11,
    'extras_id' => array()
);

//Hold data of Customer Orders 
$shoppingBasket = array(
    "ShopID_24" => array(
        'ProductID' => array(
            '12' => array(
                'OptionID' => array(
                    '4' => $option4,
                    '2' => $option2
                )
            ),
            '35' => array(
                'OptionID' => array(
                    '52' => $option52
                )
            ),
        )
    )
);

echo "<pre>";
print_r($shoppingBasket);
echo "</pre>";

print_r output:

Array
(
    [ShopID_24] => Array
        (
            [ProductID] => Array
                (
                    [12] => Array
                        (
                            [OptionID] => Array
                                (
                                    [4] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [quantity] => 2
                                                    [extras_id] => Array
                                                        (
                                                            [0] => 5
                                                            [1] => 10
                                                        )
                                                )
                                            [1] => Array
                                                (
                                                    [quantity] => 1
                                                    [extras_id] => Array
                                                        (
                                                            [0] => 2
                                                        )
                                                )

                                        )
                                    [2] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [quantity] => 1
                                                    [extras_id] => Array ()
                                                )
                                        )
                                )
                        )

                    [35] => Array
                        (
                            [OptionID] => Array
                                (
                                    [52] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [quantity] => 11
                                                    [extras_id] => Array ()
                                                )
                                        )
                                )
                        )
                )
        )
)
  • 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-26T01:06:10+00:00Added an answer on May 26, 2026 at 1:06 am

    I would consider doing this by modeling the same data in a few custom PHP objects. In this case you might have a shop object with products, and product objects with options. This is really quick off the top of my head:

    class Shop {
    
        private $_products = array();
    
        public function getProducts() 
            { return $this->_products;}
    
        public function addProduct(Product $product) 
            { $this->_products[] = $product; 
              return $this;
            }
    
    }
    
    class Product {
        private $_options = array();
    
        public function getOptions()
            { return $this->_options; }
    
        public function addOption(Option $option)
            { $this->_options[] = $option;
              return $this;
            }
    }
    
    class Option {
        private $_optionKey;
        private $_optionValue;
    
        public function getKey()
            { return $this->_optionKey; }
    
        public function getKey()
            { return $this->_optionValue; }
    
        public function setOption($key, $value)
            { 
              $this->_optionKey = $key;
              $this->_optionValue = $value;
              return $this;
            }
    }
    

    What does this get you? For starters, you can define limits and parameters to what you can store in this, while with the nested array that you are using, there is absolutely no enforcement of structure or value. You can also define other methods that allow you to actually DO things with these bits of data.

    If you absolutely MUST have an array version of these, you can implement something like a toArray() method in each of these that will convert the objects into an array to be consumed by some other bit of code. You might also consider reading up on a few interfaces such as iterator and countable in the PHP manual.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when trying to implement an Aspect, that is responsible for catching and logging a
Trying to implement some nested loops that are spitting out good old nested html
SetFocus I'm trying implement the above Se Focus code in a Class Library that
I'm trying implement Data Annotation to my Linq to SQL objects. The .dbml file
Trying to implement the following structure from c to use NSArray in objective-c: In
Trying to implement AVAudioplayer and get some metering data of the played music, but
Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however
Trying to implement an autocomplete box ultimately. For now im following php academy's lead.
When trying to implement a simple OpenGL application, I was surprised that while it
I am trying implement the Data transformation using Reflection 1 example in my code.

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.