经历一个循环并将新变量添加到多维数组中?在PHP中

时间:2018-12-02 22:10:42

标签: php arrays json multidimensional-array

总结项目;我正在建立一个网页,您可以在其中输入警报时间以及在达到该时间时要执行的操作。有我的网站脚本和python脚本。 python脚本会检查配置文件是否符合时间条件,然后执行所需的操作。

收集所有需要的信息后,我无法将其放入json文件中。每次用户输入或更改值并将其提交时,json配置文件都会被覆盖。 (为保持以前的警报时间,它们被放置在来自表单的输入值中)。警报时间每天可能不同,然后每周重复一次。 为了说明这一点,每次输入时,我都会使用所有不同的值创建很多变量。 星期一,我将有

$intMoH
$intMoM
$intMoAction
$intMoSource

每周一整天都重复一次。

然后我想将它们全部放入数组中,以便可以对其进行编码并将其更改为json格式。

json格式应该类似于这样。

[{
    "wake": [{
        "id": 1,
        "MoM": 8,
        "MoH": 30,
        "MoAction": "music",
        "MoSource": "music.mp3",
        "TuM": 7,
        "TuH": 30,
        "TuAction":"music",
        "TuSource":"music2.mp3"
    }]
}]

我没有创建一个名为Mo的新元素,然后将所有项目作为其子项,因为有人告诉我这样做会使将其放入数组更容易(尺寸较小)。

我的php页面建立一个表单,然后提交给自己(不使用ajax,仅仅是因为我不知道)。

$arrWeek = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saterday","Sunday");
$arrWeekSimple = array("Mo","Tu","We","Tu","Th","Fr","Sa","Su");
//$mowakeh = $_POST['mowakeh'];

$strName = "wake";
for( $i = 0; $i<6; $i++ )
{
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"int" . $arrWeekSimple[$i] ."H"}  = $_POST[$tempPost];
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"int" . $arrWeekSimple[$i] ."M"}  = $_POST[$tempPost];
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"str" . $arrWeekSimple[$i] ."Action"} = $_POST[$tempPost];
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"str" . $arrWeekSimple[$i] ."Source"} = $_POST[$tempPost];
/*

What values after taken from form would look like
$intMoH = 20;
$intMoM = 30;
$strMoAction = 5;
$strMoSource = 7;
*/
$arrNewJson=array();

$arrPush = array($strName=array(id=>1, $arrWeekSimple[$i]."H"=>${"int".$arrWeekSimple[$i]."H"}, $arrWeekSimple[$i]."M"=>${"int".$arrWeekSimple[$i]."M"}, $arrWeekSimple[$i]."Action"=>${"str".$arrWeekSimple[$i]."Action"},$arrWeekSimple[$i]."Source"=>${"str".$arrWeekSimple[$i]."Source"})

array_push($arrNewJson,$arrPush);


}


echo "Array made <br>";
print_r($arrNewJson);
$toTransfer = json_encode($arrNewJson, true);
file_put_contents("config.json",$toTransfer);

我所苦恼的是将所有信息都放入一个多维数组中,然后能够将其编码为json格式并替换文件。我如何每天经历并将其添加到阵列中?经过研究后,似乎由于循环,我应该使用array_push,但似乎无法理解如何使用它。 我也不知道在json文件中是否可以更轻松地进行其他操作并添加星期几?

任何帮助都非常重要。我希望所有这些都是可以理解的:-)谢谢您的时间。

1 个答案:

答案 0 :(得分:0)

这绝不是一件漂亮的事情,而且会使事情复杂化,但这确实可行。 除非您有类似的问题,否则不要打扰。

$arrWeek = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saterday","Sunday");
$arrWeekSimple = array("Mo","Tu","We","Tu","Th","Fr","Sa","Su");
//$mowakeh = $_POST['mowakeh'];



$strName = "Wake";
$arrContent=array();
$arrAlarms=array("id"=>1,"name"=>$strName);
/*

$arrContents=[0]Monday=MOM,MOH
*/
for( $i = 0; $i<6; $i++ )
{
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"int" . $arrWeekSimple[$i] ."H"}  = $_POST[$tempPost];
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"int" . $arrWeekSimple[$i] ."M"}  = $_POST[$tempPost];
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"str" . $arrWeekSimple[$i] ."Action"} = $_POST[$tempPost];
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"str" . $arrWeekSimple[$i] ."Source"} = $_POST[$tempPost];
/*

What values would look like
$intMoH = 20;
$intMoM = 30;
$strMoAction = 5;
$strMoSource = 7;
*/
echo "<br>Day?";
echo $arrWeek[$i];
${$arrWeek[$i]}= array($arrWeekSimple[$i]."H"=>${"int".$arrWeekSimple[$i]."H"}, $arrWeekSimple[$i]."M"=>${"int".$arrWeekSimple[$i]."M"}, $arrWeekSimple[$i]."Action"=>${"str".$arrWeekSimple[$i]."Action"},$arrWeekSimple[$i]."Source"=>${"str".$arrWeekSimple[$i]."Source"});
echo "<br>For this day array  ";
print_r(${$arrWeek[$i]});

//$strCurrentDay=$arrWeek[$i];
$arrContent[$arrWeek[$i]]=${$arrWeek[$i]};
//array_push($arrContent,${$arrWeek[$i]});
echo "<br>arrcontent  ";
print_r($arrContent);

}
$arrAlarms["content"]=$arrContent;

echo "Array made <br>";
print_r($arrAlarms);

结果将是这样:

Day?Monday
For this day array Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) )
Day?Tuesday
For this day array Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) )
Day?Wednesday
For this day array Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) )
Day?Thursday
For this day array Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) [Thursday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) )
Day?Friday
For this day array Array ( [ThH] => 5 [ThM] => 5 [ThAction] => 5 [ThSource] => 5 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) [Thursday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Friday] => Array ( [ThH] => 5 [ThM] => 5 [ThAction] => 5 [ThSource] => 5 ) )
Day?Saterday
For this day array Array ( [FrH] => 6 [FrM] => 6 [FrAction] => 6 [FrSource] => 6 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) [Thursday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Friday] => Array ( [ThH] => 5 [ThM] => 5 [ThAction] => 5 [ThSource] => 5 ) [Saterday] => Array ( [FrH] => 6 [FrM] => 6 [FrAction] => 6 [FrSource] => 6 ) ) Array made
Array ( [id] => 1 [name] => Wake [content] => Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) [Thursday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Friday] => Array ( [ThH] => 5 [ThM] => 5 [ThAction] => 5 [ThSource] => 5 ) [Saterday] => Array ( [FrH] => 6 [FrM] => 6 [FrAction] => 6 [FrSource] => 6 ) ) ) 

请记住以下几点: array_push()不允许您设置密钥。 $ {$ arrWeek [$ i]}是一个数组,分配给另一个键为$ arrWeek [$ i]的数组。 当使用$ arr = array(“ id” => 1,“ name” = $ name)制作数组时,不要愚蠢地忘记它始终是$ arr = array(“ id” => 1,“ name” = > $ name)。 另外,id键必须是字符串。

来源 https://www.tutorialspoint.com/php/php_get_post.htm How to push both value and key into array