购物篮的数组结构


Array structure for Shopping Basket

我正在尝试实现多维数组,持有披萨id的数据与选项和额外的id。

让我们看看下面的场景…

客户需求

  • 两个"鸡肉披萨"(ProductID:12) -"10英寸"(OptionsID:4),外加火腿和金枪鱼(ExtrasID: 5,10)

  • 一个'鸡肉披萨' (ProductID:12) - '10英寸' (OptionsID:4)与额外的甜玉米(ExtrasID: 2)

  • 一个'鸡肉披萨' (ProductID:12) - '14英寸' (OptionsID:2),不含额外的

  • 11个"素食披萨"(ProductID:35) -"7英寸"(OptionsID:52),没有额外的

查看下面匹配场景的代码…我这样做对吗?或者可以做些什么来提高它的可读性?

//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输出:

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 ()
                                                )
                                        )
                                )
                        )
                )
        )
)

我会考虑通过在几个自定义PHP对象中对相同的数据建模来实现这一点。在本例中,您可能有一个带有产品的商店对象和一个带有选项的产品对象。这是我马上想到的:

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;
        }
}

这能给你带来什么?对于初学者来说,您可以定义可以在其中存储的内容的限制和参数,而对于您正在使用的嵌套数组,绝对没有强制的结构或值。你也可以定义其他的方法,允许你用这些数据来做一些事情。

如果你绝对必须有这些对象的数组版本,你可以在这些对象中实现一个类似toArray()方法的东西,它将对象转换成一个数组,供其他代码使用。您还可以考虑阅读PHP手册中的一些接口,如迭代器和可计数接口。

在开头设置一个数组,很好。现在以正确的方式使用它。

$option['ShopID_'.$id]; //where $id is obviusly the id number;

现在用订单填充$option数组。

$option['ShopID_'.$id]['ProductId_'.$pid][] = array(
    'quantity' => 1,
    'extras_id' => array(2), //maybe a string is enough here (?) (e.g. '2,5,etc..')
    'inches' => $inches
);

$pid显然是您正在搜索的披萨Id ..

这只是一个"静态"的例子!

我建议您使用OO编程,这为您省去了很多麻烦!试试这样做:

<?php
class Extra
{
    var $id;
    var $name;
    var $amount;
    function __construct()
    {
        $this->id = 0;
        $this->name = '';
        $this->amount = 0;
    }
}
class Option
{
    var $id;
    var $name;
    function __construct()
    {
        $this->id = 0;
        $this->name = '';
    }
}
class Pizza
{
    var $id;
    var $name;
    var $options;
    var $extras;
    function __construct()
    {
        $this->id = 0;
        $this->name = '';
        $this->options = array();
        $this->extras = array();
    }
}
?>

然后测试:

<?php
$pizzas = array();
for($i=0; $i<10; $i++)
{
    $pizza = new Pizza();
    $pizza->id = $i;
    $pizza->name = 'Pizza '.$i;
    for($j=0; $j<$i; $j++)
    {
        $option = new Option();
        $option->id = $j;
        $option->name = 'Option '.$j;
        $pizza->options[] = $option;
    }
    for($k=$i; $k>0; $k--)
    {
        $extra = new Extra();
        $extra->id = $k;
        $extra->name = 'Extra '.$k;
        $extra->amount = $k;
        $pizza->extras[] = $extra;
    }
    $pizzas[] = $pizza;
}
print_r($pizzas);
?>

祝你好运