我们如何根据php中的最后一列计算数组值

时间:2016-04-07 05:28:19

标签: php arrays

下面是我的数组。在这个数组中所有零索引值(outlook,temp和play都是标题) 和1到5的索引包含值。 最后一个索引3是我的预测器。 现在我想根据0和1索引计算Last索引值yes或no。

$array1 = Array
(
        [0] => Array
        (
            [0] => 'Outlook'
            [1] => Sunny
            [2] => Sunny
            [3] => Overcast 
            [4] => Rainy
            [5] => Rainy
    )
    [1] => Array
        (
            [0] => 'Temp'
            [1] => Hot
            [2] => Hot
            [3] => Hot
            [4] => Mild
            [5] => Cool
    )
    [3] => Array
        (
            [0] => 'Play'
            [1] => No
            [2] => Yes
            [3] => Yes
            [4] => No
            [5] => Yes
    )
)

我希望输出如下。 其中我算阳光包含1是和1不等等。

Array
(
    [Outlook] => Array
        (
            [Sunny] => Array
                (
                    [No] => 1
                    [Yes] => 1
                )

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

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

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

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

我已经在下面编写了代码,但它无法正常工作

                $headingarray = array();
                $finalarrays = array();
                $last = "";
                $innerkey= "";
                foreach($array1 as $mapkey => $value){
                    $last = count($mapkey);

                    $headingarray = $mapedarray[$mapkey][0];

                    foreach($mapvalue as $mapckey => $mapcvalu){

                        $innerkey= count($mapckey);

                        for($i=0; $i<$last; $i++){


                        for($j=1; $j<count($innerkey); $j++){


                                $finalarray[$headingarray][$mapedarray[$i][$j]][$mapedarray[$last][$j]]++;
                            } 

                        }   

                    }
            }

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

1 个答案:

答案 0 :(得分:0)

这是我做的方式。我会得到最后一个数组键,然后循环遍历整个数组,使用该键设置最终的数组键和值。

//go to the last array
end($array1);
//get the last array key
$last = key($array1);
//reset back to the start of the array
reset($array1);

//array to hold the final array
$finalarrays = array();

//loop over the full array
foreach($array1 as $key=>$v1){

    //if the last array, break out of the loop
    if($last==$key)break;
    else {
        //loop over each array values
        foreach($v1 as $k2=>$v2){
            //if the 1st, set the final array key
            if($k2==0){
                $finalarrays[$v2]=array();
                $currentKey=$v2;
            }
            else {
                //use the last array key value to set the final array keys and values
                $finalarrays[$currentKey][$v2][$array1[$last][$k2]] = isset($finalarrays[$currentKey][$v2][$array1[$last][$k2]]) ? $finalarrays[$currentKey][$v2][$array1[$last][$k2]]+1: 1;
            }
        }
    }

}

在此处查看此行动 - https://3v4l.org/EqgXJ