购物篮的阵列结构

时间:2011-09-28 21:54:46

标签: php arrays oop multidimensional-array

我正在尝试使用选项和附加ID来实现包含Pizza id的数据的多维数组。

让我们看一下以下场景......

客户希望

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

  • 一个'鸡肉比萨'(ProductID:12) - '10英寸'(OptionsID:4)加上甜玉米(ExtrasID: 2

  • 一个'鸡肉比萨'(ProductID:12) - '14英寸'(OptionsID:2),没有额外内容

  • 十一'素食比萨'(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 ()
                                                )
                                        )
                                )
                        )
                )
        )
)

3 个答案:

答案 0 :(得分:3)

我会考虑通过在一些自定义PHP对象中建模相同的数据来做到这一点。在这种情况下,您可能拥有包含产品的shop对象,以及包含选项的product对象。这真的很快就脱离了我的脑海:

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手册中阅读几个接口,如迭代器和可数。

答案 1 :(得分:1)

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

$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。

这也只是一个“静态”的例子!

答案 2 :(得分:1)

我建议你使用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);
?>
祝你好运:)

相关问题