Php 如果计算两个或多个变量,则创建一组规则。从html表单

Php 如果计算两个或多个变量,则创建一组规则。从html表单,php,arrays,count,Php,Arrays,Count,我想用php创建一组规则 我有一个表单,有两个选择字段(值从1->5 Cookies are: $Field1 = intval($_POST["Field1"]); $Field2= intval($_POST["Field2"]); 我想创建一个规则,如下例所示 Field1 has 1 selected and Field2 has 2 selected if ( count($Field1) = 1) and ( count($Field2) = 2) array_push(

我想用php创建一组规则 我有一个表单,有两个选择字段(值从1->5

Cookies are:
$Field1 = intval($_POST["Field1"]);
$Field2= intval($_POST["Field2"]);
我想创建一个规则,如下例所示

Field1 has 1 selected and Field2 has 2 selected

if ( count($Field1) = 1) and ( count($Field2) = 2)
    array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));

等等

这可能吗


上面的代码不正确,但我已输入它以显示我需要的内容。

您希望得到什么结果?

如果您这样做:

$box = array();
// Field1 has 2 selected and Field2 has 3 selected
if ($Field1 == 2) and ($Field2 == 4) {
    array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
    array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
}
print_r($box);
唯一的结果是一个数组,您提交的物品数量在$Field2内

//output:
Array
(
    [0] => Array([paxType] => Cake),
    [1] => Array([paxType] => Cake),
    [2] => Array([paxType] => Cake),
    [3] => Array([paxType] => Cake)
)

由于您最近的声明:


结果应该是:一个awwat和3个蛋糕,一个arre和2个蛋糕 蛋糕。一个数组可以填充3块蛋糕那西农

以下代码将执行您期望的操作:

<?php

class boxes
{
    private $_boxes = array();
    public $maxCake = 3; // default .. 3

    public function addBox()
    {
        $this->_boxes[] = array();
    }

    public function getBoxes()
    {
        return $this->_boxes;
    }

    public function addCake($boxNum = 0, $cake = array('paxType' => 'Cake'))
    {
        if (empty($this->_boxes)) {
            $this->addBox();
        }

        if (empty($this->_boxes[$boxNum])) {
            $this->_boxes[$boxNum] = array();
        }

        // if box is full with cakes (maxCake) create a new one
        if (count($this->_boxes[$boxNum]) == $this->maxCake) {
            $oldBoxNum = $boxNum;
            // searching for a box with enough space for another cake
            foreach ($this->_boxes as $key => $box) {
                if (count($box) != $this->maxCake) {
                    $boxNum = $key;
                    break;
                }
            }
            // if we can't found one we add a new box
            if ($oldBoxNum == $boxNum) {
                $this->addBox();
                $boxNum++;
            }
        }

        $this->_boxes[$boxNum][] = $cake; 
    }
}

$Field1 = 1; // boxes
$Field2 = 5; // cakes

$boxes = new boxes();
$boxes->maxCake = 3; // if there are more than 3 cakes in a box, a new box will be created

// create number of boxes given with field1
for ($x = 0; $x < $Field1; $x++) {
    $boxes->addBox();
}

// add cakes given with field2
$y = 0;
for ($x = 0; $x < $Field2; $x++) {
    $boxes->addCake($y);
    $y++;
    if ($Field1 == $y) {
        $y = 0;
    }
}

$result = $boxes->getBoxes();
for ($x = 0; $x < count($result); $x++) {
    echo 'Box' . ($x + 1) . ':';
    echo "<br>";
    print_r($result[$x]);
    echo "<br>";
}

谢谢@stefan。我的预期结果是:例如:如果Field1选择了2个,field2选择了3个,我想推两个阵列。一个阵列有2个蛋糕,一个阵列有一个蛋糕。@RazvanBaba-好的,如果Field1选择了1个,field2选择了5个,你期望得到什么结果?结果应该是:一个awwat有3个蛋糕,一个arre with 2块蛋糕。一个数组可以填充3块蛋糕那西农。
//output:
Array
(
    [0] => Array([paxType] => Cake),
    [1] => Array([paxType] => Cake),
    [2] => Array([paxType] => Cake),
    [3] => Array([paxType] => Cake)
)
<?php

class boxes
{
    private $_boxes = array();
    public $maxCake = 3; // default .. 3

    public function addBox()
    {
        $this->_boxes[] = array();
    }

    public function getBoxes()
    {
        return $this->_boxes;
    }

    public function addCake($boxNum = 0, $cake = array('paxType' => 'Cake'))
    {
        if (empty($this->_boxes)) {
            $this->addBox();
        }

        if (empty($this->_boxes[$boxNum])) {
            $this->_boxes[$boxNum] = array();
        }

        // if box is full with cakes (maxCake) create a new one
        if (count($this->_boxes[$boxNum]) == $this->maxCake) {
            $oldBoxNum = $boxNum;
            // searching for a box with enough space for another cake
            foreach ($this->_boxes as $key => $box) {
                if (count($box) != $this->maxCake) {
                    $boxNum = $key;
                    break;
                }
            }
            // if we can't found one we add a new box
            if ($oldBoxNum == $boxNum) {
                $this->addBox();
                $boxNum++;
            }
        }

        $this->_boxes[$boxNum][] = $cake; 
    }
}

$Field1 = 1; // boxes
$Field2 = 5; // cakes

$boxes = new boxes();
$boxes->maxCake = 3; // if there are more than 3 cakes in a box, a new box will be created

// create number of boxes given with field1
for ($x = 0; $x < $Field1; $x++) {
    $boxes->addBox();
}

// add cakes given with field2
$y = 0;
for ($x = 0; $x < $Field2; $x++) {
    $boxes->addCake($y);
    $y++;
    if ($Field1 == $y) {
        $y = 0;
    }
}

$result = $boxes->getBoxes();
for ($x = 0; $x < count($result); $x++) {
    echo 'Box' . ($x + 1) . ':';
    echo "<br>";
    print_r($result[$x]);
    echo "<br>";
}
Box1:
Array (
    [0] => Array( [paxType] => Cake )
    [1] => Array( [paxType] => Cake )
    [2] => Array( [paxType] => Cake )   
)                                                      

Box2:  
Array (                                                      
    [0] => Array( [paxType] => Cake )
    [1] => Array( [paxType] => Cake )
)