Go through a loop and adding new variables into multidimensional arrays? in PHP

Solution:
This is in no way pretty and overcomplicates things but this is does work. Don't bother unless you have a similair problem.
$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);
The result would be this:
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 ) ) )
A few things to keep in mind: array_push() doesn't allow you to set a key. ${$arrWeek[$i]} is a array that gets assigned to a different array where the key is $arrWeek[$i]. When making an array with $arr=array("id"=>1,"name"=$name) don't stupidly forget a that it always is $arr=array("id"=>1,"name"=>$name). Also an id key must be a string.
Sources https://www.tutorialspoint.com/php/php_get_post.htm How to push both value and key into array
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: err_ossl_pem_no_start_line
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.