基于给定数组的Bulid for循环

时间:2016-03-22 10:23:32

标签: php arrays loops

我正在生成用于保存的动态文本框并添加更多功能,我必须将数据存储在数据库中我得到了数组但我不知道如何将该数组放入循环中以便我可以获取我的数据。

Array基于这个prepare循环看起来像这样,所以我可以访问数组的每个元素:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [prem_type] => 1
                        )

                    [1] => Array
                        (
                            [phase_name] => a1
                        )

                    [2] => Array
                        (
                            [counter] => 2
                        )

                    [3] => Array
                        (
                            [block] => A
                        )

                    [4] => Array
                        (
                            [block] => B
                        )

                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [prem_type] => 1
                        )

                    [1] => Array
                        (
                            [phase_name] => a2
                        )

                    [2] => Array
                        (
                            [counter] => 2
                        )

                    [3] => Array
                        (
                            [block] => A
                        )

                    [4] => Array
                        (
                            [block] => B
                        )

                )

        )

)

由于

3 个答案:

答案 0 :(得分:0)

要获取数据,您可以使用以下内容:

foreach ($array AS $row) {
   $prem_type = $row[0][0]['prem_type'];
   $phase_name = $row[0][1]['phase_name'];
   $counter = $row[0][2]['counter'];
   $block1 = $row[0][3]['block'];
   $block2 = $row[0][4]['block'];
}

另一种方法是将数组重组为更整洁的东西:

$result = array();
foreach ($array AS $rowId => $row) {
    $result[$rowId] = array(
        'prem_type' => $row[0][0]['prem_type'],
        'phase_name' => $row[0][1]['phase_name'],
        'counter' => $row[0][2]['counter'],
        'block1' => $row[0][3]['block'],
        'block2' => $row[0][4]['block']
    );
}

这导致:

Array
(
    [0] => Array
    (
        [prem_type] => 1,
        [phase_name] => a1,
        [counter] => 2,
        [block1] => A,
        [block2] => B
    )
    ...
)

答案 1 :(得分:0)

试试这个

$array = //your array

foreach($array as $value){
    foreach($value as $value2){
        foreach($value2 as $value3){
            foreach($value3 as $key3 => $value3){
                //$key3 is rem_type, phase_name…
                //$value3 is required values
            } 
        }
    }
} 

答案 2 :(得分:0)

以下是答案:

for($i=0;$i<count($data1);$i++){
            for($j=0;$j<count($data1[$i]);$j++){
                $prem_type =$data1[$i][$j][0]['prem_type'];
                $prem_name= $data1[$i][$j][1]['phase_name'];
                $no_of_phase= $data1[$i][$j][2]['counter'];
            echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
                    for($k=3;$k<count($data1[$i][$j]);$k++){
                            echo    $data1[$i][$j][$k]['unitname']."<br>";
                    }
            }
        }