2d数组总是索引为0

时间:2017-05-17 05:53:47

标签: php arrays mysqli

我在countrySegment.php中有这段代码:

  <tbody>
            <?php

            require 'class.php';
            require './session.php';
            $conn = new db_class();
            $read = $conn->select();
            $test = $conn->readSegments();

            while ($fetch = $read->fetch_array()) {

                ?>

                <tr>
                    <td><input type="hidden" name="country[]" value=" <?php echo $fetch['id'] ?>"/>
                        <?php echo $fetch['country'] ?>

                    </td>
                    <td><select name="segment[<?php echo $fetch['id']; ?>][]" id="">
                            <option></option>
                            <?php
                            $reads = $conn->readSegments();

                            $country_id = $fetch['id'];
                            $country_segment_id = $conn->getCountrySegment($country_id)->fetch_array();


                            while ($fetch = $reads->fetch_array()) {

                                ?>

                                <option <?php if($country_segment_id['segment_id'] == $fetch['id']){ echo "selected=\"selected\" ";}?> value="<?php echo $fetch['id'];?>"><?php echo $fetch['segment'];?></option>

                            <?php } ?>

                        </select>
                    </td>
                        <td><select name="segment[<?php echo $country_id; ?>][]" id="">

                            <?php
                            $reada = $conn->readSegments();




                            while ($fetch = $reada->fetch_array()) {
                                ?>

                                <option value="<?php echo $fetch['id'];?>" ><?php echo $fetch['segment'];?></option>

                            <?php } ?>

                        </select>
                    </td>
                </tr>

            <?php }


            ?>


            </tbody>

这是帖子的行动:

<?php print_r($_POST['segment']);require_once "class.php";if (isset($_POST['country'])) {

$countrys = $_POST['country'];
$segments = $_POST['segment'];  foreach ($countrys as $keyC => $country) {
    foreach ($segments as $keyS => $segment) {

        if ($keyC == $keyS) {
            // logic go here

            $db = new db_class();
           $db->selectCountrySegment($country, $segment);
           echo '


    ';
        }

    }
}

$allKeys = array_keys($segments);
// echo $allKeys[0];


for ($i = 0; $i < count($segments); $i++) {
    echo "Country id =".$allKeys[$i]." has:";
    $country=$allKeys[$i];
    for ($l = 0; $l < count($segments[$i]); $l++) {
        echo "segment".$segments[$i][$l];
        $segment= $segments[$i][$l];
        $db = new db_class();
        $db->selectCountrySegment($country, $segment);
    }

}

我有这个输出:

Array ( [1] => Array ( [0] => 2 [1] => 20 ) [2] => Array ( [0] => 1 [1] => 20 ) [3] => Array ( [0] => 3 [1] => 20 ) [4] => Array ( [0] => 1 [1] => 20 ) [5] => Array ( [0] => 1 [1] => 2 ) [6] => Array ( [0] => 1 [1] => 20 ) [7] => Array ( [0] => 1 [1] => 20 ) [8] => Array ( [0] => 1 [1] => 20 ) [9] => Array ( [0] => 1 [1] => 20 ) [10] => Array ( [0] => 1 [1] => 20 ) ) 

所以问题始终是数组的索引是0,并且它总是为所有

采用相同的段

然后我有这两条通知:Notice: Undefined offset: 0在这一行:for ($l = 0; $l < count($segments[$i]); $l++) {

请帮助我第一次使用2D阵列谢谢你

2 个答案:

答案 0 :(得分:0)

$ segments数组从1开始,因此$ segments [0]

中没有元素
Array ( [1] => Array ( [0] => 2 [1] => 20 ) [2] => Array ( [0] => 1 [1] => 20 ) [3] => Array ( [0] => 3 [1] => 20 ) [4] => Array ( [0] => 1 [1] => 20 ) [5] => Array ( [0] => 1 [1] => 2 ) [6] => Array ( [0] => 1 [1] => 20 ) [7] => Array ( [0] => 1 [1] => 20 ) [8] => Array ( [0] => 1 [1] => 20 ) [9] => Array ( [0] => 1 [1] => 20 ) [10] => Array ( [0] => 1 [1] => 20 ) ) 

您可以将其更改为从1开始

for ($i = 0; $i < count($segments); $i++) {
echo "Country id =".$allKeys[$i]." has:";
$country=$allKeys[$i];
    for ($l = 0; $l < count($segments[$i+1]); $l++) {
        echo "segment".$segments[$i+1][$l];
        $segment= $segments[$i+1][$l];
        $db = new db_class();
        $db->selectCountrySegment($country, $segment);
    }
}

答案 1 :(得分:0)

您的$segments数组以索引0开头,请将您的代码更改为:

$segments = array(
    1 => array(1, 2),
    2 => array(3, 4),
    3 => array(5, 6),
    4 => array(7, 8),
    5 => array(9, 10)
);

$allKeys = array_keys($segments);

for ($i = 1; $i <= count($segments); $i++) {
//       ^^^
    echo "Country id =".$allKeys[$i-1]." has:";
//                               ^^^^
    $country=$allKeys[$i-1];
//                    ^^^^
    for ($l = 0; $l < count($segments[$i]); $l++) {
        echo "segment".$segments[$i][$l]."<br>";
        $segment= $segments[$i][$l];
        $db = new db_class();
        $db->selectCountrySegment($country, $segment);
    }

}

如果您从0开始count($segments[$i])(在for ($l = 0; $l < count($segments[$i]); $l++) {中)将抛出错误,因为$segments[0]不存在。

我建议将count($segments)放在for循环之外,这样可以缩短脚本的执行时间。请参阅PHP benchmark for-loop test