如何在PHP中制作数组的静态索引


how to make static index of array in php

我正在尝试将值从表单无线电存储到数组。但是我面临的问题是每次数组 1st 索引都被新值替换。我认为这是范围问题。我也试图声明全局数组,但没有成功。

这是代码:

<?php
include_once("connection.php");
$c = new DBcon();
$c->startcon();
global $array; // not effecive
//  $q = $_POST['t'];
//  echo 'fff', $q;

$page = $_GET['page'];
echo 'pagesss', $page, 'ppp';
if ($page == "") {
    $page = "1";
} else {
    // If page is set, let's get it
    $page = $_GET['page'];
}
// Now lets get all messages from your database
$sql = "SELECT * FROM quizes";
$query = mysql_query($sql);
// Lets count all messages
$num = mysql_num_rows($query);
// Lets set how many messages we want to display
$per_page = "2";
// Now we must calculate the last page
$last_page = $num;
echo 's', $num;
// And set the first page
$first_page = "1";
// Here we are making the "First page" link
echo "<a href='?page=" . $first_page . "'>First page</a> ";
// If page is 1 then remove link from "Previous" word
if ($page == $first_page) {
    echo "Previous ";
} else {
    if (!isset($page)) {
        echo "Previous ";
    } else {
        // But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
        $previous = $page - 1;
        echo "<a href='?page=" . $previous . "'>Previous</a> ";
    }
}
// If the page is last page.. lets remove "Next" link
if ($page == $last_page) {
    echo "Next ";
} else {
    // If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
    if (!isset($page)) {
        $next = $first_page + 1;
        echo "<a href='?page=" . $next . "'>Next</a> ";
    } else {
        $next = $page + 1;
        echo "<a href='?page=" . $next . "'>Next</a> ";
    }
}
// And now lets add the "Last page" link
echo "<a href='?page=" . $last_page . "'>Last page</a>";
// Math.. It gets us the start number of message that will be displayed
$start = ($page * ($page - 1)) / $page;
echo 'start', $start;
echo 'page', $page;
// Now lets set the limit for our query
$limit = "LIMIT $start, $per_page";
// It's time for getting our messages
$sql = "SELECT * FROM quizes $limit";
$query = mysql_query($sql);
echo "<br /><br />";
// And lets display our messages
$i = 0;
$l = 0;
while ($row = mysql_fetch_array($query) or die(mysql_error())) {
    $a = $row['A'];
    echo '<form method="get" action="?page=".$next."">';
    while ($row = mysql_fetch_array($query)) {
        echo '<div class="boxed" >';
        echo "'t" . '<tr><th>' .
        $row['question'] . "<br>" .
        '</th><th>' . "<input type='radio' name= 't[]' value='{$row['A']}'>" . $row['A'] . "<br>" .
        '</th><th>' . "<input type='radio' name='t[]' value='{$row['B']}'>" . $row['B'] . "<br>" .
        '</th><th>' . "<input type='radio' name='t[]' value='{$row['C']}'>" . $row['C'] . "<br>" .
        '</th><th>' . "<input type='radio' name='t[]' value='{$row['D']}'>" . $row['D'] . '</th>
                </tr>';
        echo '<input type="hidden" name="page" value="' . $next . '">';
        echo '<input type="submit" name="submit"/>';
        $i++;
        echo '</div>';

        echo '</div>';
    }
    echo '</form>';
    if (isset($_GET['submit'])) {
        $example = $_GET['t'];
        foreach ($example as $value) {
            $array[$i++] = ($value);
            echo "$array[0] <br>"; // printing correct statement but replacing old values with new value everytime.
            echo "$array[1] <br>"; // 0 values
            echo "$array[2] <br>"; // 0 values
        }
    }
}
?>
我看过这些帖子:PHP 数组索引:$array[$index] vs $array["$index"]

vs $array["{$index}"],PHP - 定义对象的静态数组但没有帮助。请帮忙我该怎么办?

你不能做

$array[$i++]

这将永远是$array[1]而是做

$i++;
$array[$i]= $value;

附言$array是一个变量的可怕名称......

if (isset($_GET['submit'])) {
        $example = $_GET['t'];
$i=0;
$arrayA = array();
        foreach ($example as $value) {
            $arrayA[$i] = ($value);
$i++;
        }
print_r($arrayA);
    }

如果你这样做

$array[$i++] ;

不起作用,只需尝试一下,它不会取代您的旧值

$array[$i] = $value;    
    $i++;

必须先递增变量,然后将变量用作数组键。

编辑:这是您要查找的内容:

$i = 0; 
foreach ($example as $value) {
    $array[$i] = $value;    
    $i++;
}