如何在PHP中一次一行地填充关联数组


How do I populate an associative array in PHP one line at a time?

我有这个数组:

$crops[] = array(
   'Type' => $type_name,
   'Method' => $method_name,
   'Crop' => $crop_name,
   'SowDirect' => $direct_sow_date,
   'SowTransplant' => $transplant_sow_date,
   'Transplant' => $transplant_date,
   'BeginHarvest' => $begin_harvest_date,
   'EndHarvest' => $end_harvest_date
);

我想要的是在一些逻辑代码中逐个添加键/值对:

$crops[] = array();
if($var1 == something) {
   $crops['Type'] = $value1;
}
if($var2 == something) {
   $crops['Method'] = $value2;
}else{
   $crops['Crop'] = $value3;
}

使用第一种方法,我得到了这个,这就是我想要的:http://gardencalc.drivingpeace.com/grid_data.php

使用第二种方式,我得到了这个,这不是我想要的:http://gardencalc.drivingpeace.com/grid_data1.php

如何使示例2与示例1工作相同?

更新4/18/14

下面的答案成功了。我遇到的主要问题(也是我希望你的建议能解决的问题)是,我在逻辑决策中操纵了一堆日期,我使用的是date_add()&date_sub()。

如果我只是在代码中打印日期,它会很好地工作,我会得到我想要的更改。

但是,如果我操作日期,像在您的方法中那样将它们添加到数组中,然后通过JSON打印数组,则日期都不会更改。

这是您修改后的数组代码,其中日期仍然不变:

<?php
$loc_id = 174;
$prob_id = 10;
#Include the connect.php file
include('connect.php');
#Connect to the database
//connection String
$connect = mysql_connect($hostname, $username, $password)
or die('Could not connect: ' . mysql_error());
//select database
mysql_select_db($database, $connect);
//Select The database
$bool = mysql_select_db($database, $connect);
if ($bool === False){
print "can't find $database";
}
$tempSQL = "SELECT * FROM prob_28 WHERE prob_id=$loc_id";
$temp = mysql_query($tempSQL) or die(mysql_error());
while($row = mysql_fetch_array( $temp )) {
$prob1_90 = $row['prob1_90'];
$prob1_50 = $row['prob1_50'];
$prob1_10 = $row['prob1_10'];
$prob2_10 = $row['prob2_10'];
$prob2_50 = $row['prob2_50'];
$prob2_90 = $row['prob2_90'];
}
//build the dates
if ($prob_id == "10"){
$first_frost_date = date_create(date("Y").'-'.substr($prob1_10,0,2).'-'.substr($prob1_10,2,2));
$last_frost_date = date_create(date("Y").'-'.substr($prob2_10,0,2).'-'.substr($prob2_10,2,2));
}elseif ($prob_id == "50"){
$first_frost_date = date_create(date("Y").'-'.substr($prob1_50,0,2).'-'.substr($prob1_50,2,2));
$last_frost_date = date_create(date("Y").'-'.substr($prob2_50,0,2).'-'.substr($prob2_50,2,2));
}else{
$first_frost_date = date_create(date("Y").'-'.substr($prob1_90,0,2).'-'.substr($prob1_90,2,2));
$last_frost_date = date_create(date("Y").'-'.substr($prob2_90,0,2).'-'.substr($prob2_90,2,2));
}

// get data and store in a json array
$query = "SELECT * FROM data ORDER BY data_id";
$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$curcrop = array();
$data_id = $row['data_id'];
$days_to_harvest = $row['days_2_harvest'];
$resolution = $row['resolution'];
//Get type name
$type_id = $row['type_id'];
$type = mysql_query("SELECT * FROM type WHERE type_id=$type_id") or die(mysql_error());
while($typerow = mysql_fetch_array( $type )) {
$curcrop['Type'] = $typerow['type_name'];
}
//Get planting method name
$method_id = $row['method_id'];
$method = mysql_query("SELECT * FROM method WHERE method_id=$method_id") or die(mysql_error());
while($methodrow = mysql_fetch_array( $method )) {
$curcrop['Method'] = $methodrow['method_name'];
}
//Get crop name
$crop_id = $row['crop_id'];
$crop = mysql_query("SELECT * FROM crop WHERE crop_id=$crop_id") or die(mysql_error());
while($croprow = mysql_fetch_array( $crop )) {
$curcrop['Crop'] = $croprow['crop_name'];
}
//calculate the direct sow date
//Only build real date if the planting method is direct sow
if ($method_id == 1){
//check if it's a fall or spring crop type
//spring date
if ($type_id == 1){
$direct_sow_date = $first_frost_date;
$date_int = $row['wks_b4_frost_2_sow'] * 7;
date_sub($direct_sow_date,date_interval_create_from_date_string("$date_int days"));
//fall date
}else{
$direct_sow_date = $last_frost_date;
$date_int = $row['wks_b4_first_frost'] * 7;
date_sub($direct_sow_date,date_interval_create_from_date_string("$date_int days"));
}
}else{
$direct_sow_date = null;
}

$curcrop['SowDirect'] = $direct_sow_date;

//calculate transplant date
if ($method_id == 2){
//check if it's a fall or spring crop type
//spring date
if ($type_id == 1){
$transplant_date = $first_frost_date;
$date_int = $row['wks_b4_last_frost_2_trans'] * 7;
date_sub($transplant_date,date_interval_create_from_date_string("$date_int days"));
//fall date
}else{
$transplant_date = $last_frost_date;
$date_int = $row['wks_b4_first_frost'] * 7;
date_sub($transplant_date,date_interval_create_from_date_string("$date_int days"));
}
}else{
$transplant_date = null;
}
//transplant sow date
if ($transplant_date != null){
$date_int_trans = $row['wks_2_grow_trans'] * 7;
date_sub($transplant_date,date_interval_create_from_date_string("$date_int_trans days"));
$curcrop['SowTransplant'] = $transplant_date;
date_add($transplant_date,date_interval_create_from_date_string("$date_int_trans days"));
}else{
$curcrop['SowTransplant'] = null;
}

$curcrop['Transplant'] = $transplant_date;

//begin harvest date
if ($method_id == 1 && $direct_sow_date != null){
date_add($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_sub($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
$curcrop['BeginHarvest'] = $direct_sow_date;
date_sub($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_add($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
}
if ($method_id == 2 && $transplant_date != null){
date_add($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_sub($transplant_date,date_interval_create_from_date_string("$resolution days"));
$curcrop['BeginHarvest'] = $transplant_date;
date_sub($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_add($transplant_date,date_interval_create_from_date_string("$resolution days"));
}
//end harvest date
if ($method_id == 1 && $direct_sow_date != null){
date_add($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_add($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
$curcrop['EndHarvest'] = $direct_sow_date;
date_sub($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_sub($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
date_add($direct_sow_date,date_interval_create_from_date_string("$date_int days"));
}
if ($method_id == 2 && $transplant_date != null){
date_add($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_add($transplant_date,date_interval_create_from_date_string("$resolution days"));
$curcrop['EndHarvest'] = $transplant_date;
date_sub($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_sub($transplant_date,date_interval_create_from_date_string("$resolution days"));
date_add($transplant_date,date_interval_create_from_date_string("$date_int days"));
}

$crops[] = $curcrop;
}

echo json_encode($crops);
?>

在第一个示例中,将一个信息数组附加到$crops数组,但在第二个示例中将一个空数组添加到$crops数组,然后在同一个$crops数组中设置几个键。

您需要做的是使用第二种方法构建"当前作物"数组,然后将该数组附加到$crops列表中,如前所述:

$curcrop = array();
if($var1 == something) {
   $curcrop['Type'] = $value1;
}
if($var2 == something) {
   $curcrop['Method'] = $value2;
}else{
   $curcrop['Crop'] = $value3;
}
$crops[] = $curcrop;