我们如何检查一个数组元素并在php中放入另一个数组行

Below is my Rules Array in this array outlook,temp. For exapample Outlook contain values sunny,overcase and rainy and also have yes or no values.

$Rules = Array
(
[Outlook] => Array
    (
        [Sunny] => Array
            (
                [No] => 2
            )

        [Overcast ] => Array
            (
                [Yes] => 1
            )

        [Rainy] => Array
            (
                [Yes] => 1
            )

    )

[Temp] => Array
    (
        [Hot] => Array
            (
                [No] => 2
                [Yes] => 1
            )

        [Mild] => Array
            (
                [Yes] => 1
            )

    )

)

and below is my $filling array and i want to fill the values of yes and no according to the rules array. first i pick the all first element row wise example we pick outlook -> sunny , temp -> hot and then check in the above array by heading and count yes no values and which value of yes or no greater then we put in play field respectively.

$filling = Array
(
[Outlook] => Array
    (

        [0] => Sunny
        [1] => Sunny
        [2] => Overcast 
        [3] => Rainy
    )

[Temp] => Array
    (

        [0] => Hot
        [1] => Hot
        [2] => Hot
        [3] => Mild
    )
 [play] => Array
    (

        [0] =>
        [1] =>
        [2] => 
        [3] => 
    )
)

Example: enter image description here

You can check by running online scripts

Your Array:

$Rules = array(
    "Outlook" => array("Sunny" => array("No" => 2),
                       "Overcast" => array("Yes" => 1),
                       "Rainy" => array("Yes" => 1)
                ),
    "Temp" => array("Hot" => array("No" => 2, "Yes" => 1),
                   "Mild" => array("Yes" => 1)
                )

);

New array with condition

$filling = array();
$fill_play = array();

foreach($Rules as $key => $value){
    $in = 0;
    $in_play = 0;
    foreach($value as $keys => $values){
        $key_value = array_keys($values);
        for($j = 0; $j < count($key_value); $j++){
            for($i = 0; $i < $values[$key_value[$j]]; $i++){
                $fill_play[$key][$in_play++] = $key_value[$j];
                $filling[$key][$in++] = $keys;
            }
        }
    }       
}
$yes_no = array();
for($i = 0; $i < 4; $i++)
    $yes_no[$i] = 0;

foreach($fill_play as $key => $value){
    for($i = 0; $i < 4; $i++){
        if($value[$i] == 'No')
            $yes_no[$i] -= 1;
        else
            $yes_no[$i] += 1;
    }       
}

foreach($yes_no as $key => $value){
    if($value >= 0)
        $filling['Play'][$key] = 'Yes';
    else
        $filling['Play'][$key] = 'No';
}

Output

echo '<pre>';
print_r($filling);
echo '</pre>';

Result

Array
(
    [Outlook] => Array
        (
            [0] => Sunny
            [1] => Sunny
            [2] => Overcast
            [3] => Rainy
        )

    [Temp] => Array
        (
            [0] => Hot
            [1] => Hot
            [2] => Hot
            [3] => Mild
        )

    [Play] => Array
        (
            [0] => No
            [1] => No
            [2] => Yes
            [3] => Yes
        )

)