我有一个文本字段,您可以在其中添加逗号分隔值:Address1,Adress2
点击“提交”后,系统会针对您输入的每个地址返回相关信息。
我想将信息视为数组,并使用用户输入的分隔值作为数组中的键。
我有以下代码:
if(isset($_POST['submit']))
{
$addresses = explode(",", $_POST['addresses']);
$ort = $_POST['omrade'];
$obj = array();
foreach($addresses as $address)
{
echo $newSpider->fetchPage($address, $ort, $offst=0);
if(!isset($obj[$address]))
{
$obj[$address] = array();
echo "<pre>";
var_dump($address);
echo "</pre>";
}
}
}
当我输入两个值Adress1和Adress2时,var_dump的输出为:
array(1) {
["Address1"]=>
array(0) {
}
}
array(2) {
["Address1"]=>
array(0) {
}
["Address2"]=>
array(0) {
}
}
我希望它是这样的:
array(1) {
["Address1"]=>
array(0) {
}
["Address2"]=>
array(0) {
}
}
我做错了什么?有谁能帮我解释一下?
答案 0 :(得分:0)
您的代码基本上是每个循环的var_dumps。循环后只需var_dump。试试这个
if(isset($_POST['submit']))
{
$addresses = explode(",", $_POST['addresses']);
$ort = $_POST['omrade'];
$obj = array();
foreach($addresses as $address)
{
echo $newSpider->fetchPage($address, $ort, $offst=0);
if(!isset($obj[$address]))
{
$obj[$address] = array();
echo "<pre>";
echo "</pre>";
}
}
var_dump($address);
}