Php 2d数组始终索引为0

Php 2d数组始终索引为0,php,arrays,mysqli,Php,Arrays,Mysqli,我在countrySegment.php中有以下代码: <tbody> <?php require 'class.php'; require './session.php'; $conn = new db_class(); $read = $conn->select(); $test = $conn->readSegm

我在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>
所以问题总是在于数组的索引为0,并且它总是对所有数组使用相同的段

然后我有两个通知:
Notice:Undefined offset:0
在这一行:
for($l=0;$l


这是我第一次使用2d数组,请帮忙。谢谢你的$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);
    }
}
for($i=0;$i选择CountrySegment($country,$segment);
}
}

您的
$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);
    }

}
$segments=数组(
1=>数组(1,2),
2=>数组(3,4),
3=>数组(5,6),
4=>数组(7,8),
5=>数组(9,10)
);
$allKeys=数组_键($segments);
对于($i=1;$i选择CountrySegment($country,$segment);
}
}
如果以
0
count($segments[$i])
(在
for($l=0;$l
)将抛出错误,因为
$segments[0]
不存在


我建议将
count($segments)
置于for循环之外,这样可以提高脚本的执行时间。请参阅。

您的(外部)数组没有索引为
0的元素。导致错误的确切行是什么?这是:$l=0;$lfor ($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);
    }
}
$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);
    }

}