<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
if(isset($_POST['calculate'])) {
// creating an array
$error = array();
if((isset($_POST['buyer']) && is_numeric($_POST['buyer'])) || (isset($_POST['seller']) && is_numeric($_POST['seller']))) {
// setting index
$error ['buyer'] ['0'] = "the byer or seller is not numeric";
$error ['seller'] ['0'] = "the seller is not numericc";
} elseif((isset($_POST['buyer']) && !empty($_POST['buyer'])) || (isset($_POST['seller']) && !empty($_POST['seller']))) {
$buyer = $_POST['buyer'];
$seller = $_POST['seller'];
// here the error comes
} else {
$error ['buyer'] ['1'] = "please input the buyer name";
$error ['seller'] ['1'] = "please input the seller name";
}
if(empty($_POST['product'])) {
$error ['product'] ['0'] = "Please select a Product";
}
// so finally print the array shows the index which i didn't want to start from 1
echo "<pre>";
print_r($error);
echo "</pre>";
// the buyer associative array starts indexing from1
}
?>
之所以会发生这种情况,是因为这是预期的行为。观察到的行为符合规范。
参考:http://php.net/types.array
摘录:
键可以是整数,也可以是字符串。该值可以是任何类型。
此外,还会发生以下密钥转换:
- 包含有效整数的字符串将强制转换为整数类型。例如,键";8〃;将实际存储在8以下