我在php中有一个带有foreach的JSON解码数组,每次从这个下拉列表中选择一个选项时,它都会在表单中包含一个select选项,它会返回列表中的最后一个值。我试图保存平面数字,构建,解决1个变量。我该怎么做?
$api_url = 'http://rest.api?postcode=' . urlencode($_GET['postcode']);
$api_json = file_get_contents($api_url);
$api_array = json_decode($api_json, true);
$address = $api_array['addresses'];
foreach($address as $addresses){
echo '<option>'.$addresses['address'].'</option>';
$flatnumber = $addresses['flatNumber'];
$address1 = $addresses['building'];
$address2 = $addresses['address1'];
$town = $addresses['town'];
$county = $addresses['county'];
}
这是数组:
Array ( [links] => Array ( [0] => Array ( [queries] => Array ( [0] => Array ( [name] => searchByPostcode [parameters] => Array ( [0] => Array ( [data] => GL50 2Tj [name] => postcode [validateAs] => /validate/ukPostcode ) ) ) ) [rel] => /rels/domestic/address-lookup /rels/self [uri] => http://?b=2jmj7l5rSw0yVb_vlWAYkK_YBwk{&postcode} ) [1] => Array ( [rel] => /rels/home [uri] => http:// ) ) [addresses] => Array ( [0] => Array ( [address] => 22, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 22 [county] => Gloucestershire [deliveryPointSuffix] => 1A [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => [houseName] => [houseNumber] => 22 [id] => |22||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|22|||1Al|ElecM|NoGasM|NoIGT| [igtWarning] => Unknown [knownElectricityMeter] => 1 [knownGasMeter] => [name] => 22, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) [1] => Array ( [address] => 24, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 24 [county] => Gloucestershire [deliveryPointSuffix] => 1L [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => [houseName] => [houseNumber] => 24 [id] => |24||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|24|||1Lw|ElecM|GasM|NoIGT| [igtWarning] => NoIGT [knownElectricityMeter] => 1 [knownGasMeter] => 1 [name] => 24, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) [2] => Array ( [address] => Flat Above, 24-26, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 24-26 [county] => Gloucestershire [deliveryPointSuffix] => [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => Flat Above [houseName] => [houseNumber] => 24-26 [id] => Flat Above|24-26||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|24-26||||ElecM|NoGasM|NoIGT| [igtWarning] => Unknown [knownElectricityMeter] => 1 [knownGasMeter] => [name] => Flat Above, 24-26, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) [3] => Array ( [address] => 24-26, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 24-26 [county] => Gloucestershire [deliveryPointSuffix] => 1B [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => [houseName] => [houseNumber] => 24-26 [id] => |24-26||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|24-26|||1Bm|ElecM|NoGasM|NoIGT| [igtWarning] => Unknown [knownElectricityMeter] => 1 [knownGasMeter] => [name] => 24-26, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) [4] => Array ( [address] => 26, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 26 [county] => Gloucestershire [deliveryPointSuffix] => 1N [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => [houseName] => [houseNumber] => 26 [id] => |26||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|26|||1Ny|NoElecM|GasM|NoIGT| [igtWarning] => NoIGT [knownElectricityMeter] => [knownGasMeter] => 1 [name] => 26, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) [5] => Array ( [address] => 28, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 28 [county] => Gloucestershire [deliveryPointSuffix] => 1E [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => [houseName] => [houseNumber] => 28 [id] => |28||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|28|||1Ep|ElecM|NoGasM|NoIGT| [igtWarning] => Unknown [knownElectricityMeter] => 1 [knownGasMeter] => [name] => 28, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) [6] => Array ( [address] => 30, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 30 [county] => Gloucestershire [deliveryPointSuffix] => 1F [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => [houseName] => [houseNumber] => 30 [id] => |30||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|30|||1Fq|ElecM|GasM|NoIGT| [igtWarning] => NoIGT [knownElectricityMeter] => 1 [knownGasMeter] => 1 [name] => 30, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) [7] => Array ( [address] => 32, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 32 [county] => Gloucestershire [deliveryPointSuffix] => 1G [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => [houseName] => [houseNumber] => 32 [id] => |32||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|32|||1Gr|ElecM|GasM|NoIGT| [igtWarning] => NoIGT [knownElectricityMeter] => 1 [knownGasMeter] => 1 [name] => 32, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) [8] => Array ( [address] => Phoenix Inn, 36, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 36 [county] => Gloucestershire [deliveryPointSuffix] => [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => Phoenix Inn [houseName] => [houseNumber] => 36 [id] => Phoenix Inn|36||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|36||||NoElecM|GasM|NoIGT| [igtWarning] => NoIGT [knownElectricityMeter] => [knownGasMeter] => 1 [name] => Phoenix Inn, 36, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) [9] => Array ( [address] => 36, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [address1] => Andover Road [building] => 36 [county] => Gloucestershire [deliveryPointSuffix] => 1P [dependentLocality] => [dependentThorofare] => [doubleDependentLocality] => [flatNumber] => [houseName] => [houseNumber] => 36 [id] => |36||Andover Road||CHELTENHAM|Gloucestershire|GL50 2TJ|36|||1P0|ElecM|NoGasM|NoIGT| [igtWarning] => Unknown [knownElectricityMeter] => 1 [knownGasMeter] => [name] => 36, Andover Road, CHELTENHAM, Gloucestershire, GL50 2TJ [postCode] => GL50 2TJ [town] => CHELTENHAM ) ) )
答案 0 :(得分:0)
我所做的是将所有值放入选项值,提交表单然后在提交后展开值。
$result = $_POST['address1'];
$result_explode = explode('|', $result);
$flatnumber = $result_explode[0];
$building = $result_explode[1];
$street = $result_explode[2];
<form method="post">
<select name="address1">
foreach($address as $addresses){
echo '<option value="'.$addresses['flatNumber'].'|'.$addresses['building'].'|'.$addresses['address1'].'|'.$addresses['knownElectricityMeter'].'|'.$addresses['knownGasMeter'].'">'.$addresses['flatNumber'].' '.$addresses['building'].' '.$addresses['address1'].'</option>';
}
</select>
</form>