2d array always index to 0

By adamspaul

i have this code in 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>

and this is the action where the post goes:

<?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);
    }
}

and i have this output:

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 ) )

so the problem is always the index of the array is 0, and it always take same segments for all

and then i have these 2 notices:Notice: Undefined offset: 0

please help its my first time i use 2d arrays thank u

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273