尝试最终得到7个数组($mon、$tue、$wed、$thu、$fri、$sat、$sun),在给定初始数组$hours的情况下,一天中每小时的值为0或1$小时数只是一个发布的数组,包含用户为一周中的每一天选择的小时数。
我认为问题在于我使用变量($$day),因为我的工作日数组都返回null($mon,$tue,$wed,$thu,$fri,$sat,$sun)。
$hours =
Array
(
[mon] => Array
(
[0] => 12pm
[1] => 1pm
[2] => 2pm
[3] => 3pm
[4] => 4pm
[5] => 5pm
[6] => 6pm
[7] => 7pm
)
[tue] => Array
(
[0] => 12am
[1] => 1am
[2] => 2am
[3] => 3am
[4] => 4am
[5] => 5am
[6] => 6am
[7] => 7am
[8] => 8am
[9] => 9am
[10] => 10am
[11] => 11am
[12] => 12pm
[13] => 1pm
[14] => 2pm
[15] => 3pm
[16] => 4pm
[17] => 5pm
[18] => 6pm
[19] => 7pm
[20] => 8pm
[21] => 10pm
[22] => 11pm
)
[sun] => Array
(
[0] => 12am
)
)
代码:
//create an array of weekdays
$weekdays = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');
foreach($weekdays as $day)
{
//set hours for each day using variable variables $$day = mon, tue, wed, etc
if($hours[$day])
{
$$day['12a'] = in_array("12am", $hours[$day]) == 1 ? 1 : 0;
$$day['1a'] = in_array("1am", $hours[$day]) == 1 ? 1 : 0;
$$day['2a'] = in_array("2am", $hours[$day]) == 1 ? 1 : 0;
$$day['3a'] = in_array("3am", $hours[$day]) == 1 ? 1 : 0;
$$day['4a'] = in_array("4am", $hours[$day]) == 1 ? 1 : 0;
$$day['5a'] = in_array("5am", $hours[$day]) == 1 ? 1 : 0;
$$day['6a'] = in_array("6am", $hours[$day]) == 1 ? 1 : 0;
$$day['7a'] = in_array("7am", $hours[$day]) == 1 ? 1 : 0;
$$day['8a'] = in_array("8am", $hours[$day]) == 1 ? 1 : 0;
$$day['9a'] = in_array("9am", $hours[$day]) == 1 ? 1 : 0;
$$day['10a'] = in_array("10am", $hours[$day]) == 1 ? 1 : 0;
$$day['11a'] = in_array("11am", $hours[$day]) == 1 ? 1 : 0;
$$day['12p'] = in_array("12pm", $hours[$day]) == 1 ? 1 : 0;
$$day['1p'] = in_array("1pm", $hours[$day]) == 1 ? 1 : 0;
$$day['2p'] = in_array("2pm", $hours[$day]) == 1 ? 1 : 0;
$$day['3p'] = in_array("3pm", $hours[$day]) == 1 ? 1 : 0;
$$day['4p'] = in_array("4pm", $hours[$day]) == 1 ? 1 : 0;
$$day['5p'] = in_array("5pm", $hours[$day]) == 1 ? 1 : 0;
$$day['6p'] = in_array("6pm", $hours[$day]) == 1 ? 1 : 0;
$$day['7p'] = in_array("7pm", $hours[$day]) == 1 ? 1 : 0;
$$day['8p'] = in_array("8pm", $hours[$day]) == 1 ? 1 : 0;
$$day['9p'] = in_array("9pm", $hours[$day]) == 1 ? 1 : 0;
$$day['10p'] = in_array("10pm", $hours[$day]) == 1 ? 1 : 0;
$$day['11p'] = in_array("11pm", $hours[$day]) == 1 ? 1 : 0;
}
}
//debugging only to see variable values
file_put_contents('/home/test/public_html/file.txt', print_r($hours, true));
file_put_contents('/home/test/public_html/file-mon.txt', print_r($mon, true));
file_put_contents('/home/test/public_html/file-tue.txt', print_r($tue, true));
file_put_contents('/home/test/public_html/file-wed.txt', print_r($wed, true));
file_put_contents('/home/test/public_html/file-thu.txt', print_r($thu, true));
file_put_contents('/home/test/public_html/file-fri.txt', print_r($fri, true));
file_put_contents('/home/test/public_html/file-sat.txt', print_r($sat, true));
file_put_contents('/home/test/public_html/file-sun.txt', print_r($sun, true));
编辑-使用的结束解决方案:
对脚本的其他方面做了一些必要的修改。
//decode the json array posted
$hours = json_decode($_POST['hours'], true);
//set array of weekdays
$weekdays = array(1 => 'mon', 2 => 'tue', 3 => 'wed', 4 => 'thu', 5 => 'fri', 6 => 'sat', 7 => 'sun');
//set array of hour values
$hourDefs = array('12am', '1am', '2am', '3am', '4am', '5am', '6am', '7am', '8am', '9am', '10am', '11am', '12pm', '1pm', '2pm', '3pm', '4pm', '5pm', '6pm', '7pm', '8pm', '9pm', '10pm', '11pm');
//set result output array
$output = array();
foreach($weekdays as $day=>$day_value)
{
//set hours for each day using variable variables ${$day} = mon, tue, wed, etc
if(isset($hours[$day_value]))
{
foreach($hourDefs as $def)
{
$output[$day_value][$def] = in_array($def, $hours[$day_value]) == 1 ? 1 : 0;
}
}
}
我会这样重组代码:
// keys; you can also use a for() loop to generate them
$hourDefs = array('12am', '1am', '2am' ... );
$output = array();
foreach($weekdays as $day){
if(!isset($hours[$day]))
continue;
// store the output inside an array so you can loop it later
foreach($hourDefs as $def)
$output[$day][$def] = in_array($def, $hours[$day]) == 1 ? 1 : 0;
}
foreach($output as $name => $data){
$pathName = sprintf('/home/test/public_html/file-%s.txt', $name);
file_put_contents($pathName, print_r($data, true));
}
如果您想保持键名之间的不一致,请使用substr($def,0,-1)
on$out
主要内容:实例,与代数类似,PHP 变量,创建(声明)PHP 变量,实例,PHP 是一门弱类型语言,PHP 变量作用域,局部和全局作用域,实例,PHP global 关键字,实例,实例,Static 作用域,实例,参数作用域,实例变量是用于存储信息的"容器": 实例 <?php $x=5; $y=6; $z=$x+$y; echo $z; ?> 运行实例 » 与代数类似 x=5 y=6 z=x+y 在代数中,我们使用字母(如 x),并给它赋值(如 5)。 从上面的表达式 z=x+y ,我们可以计
对于变量和参数,不管是已经敲代码多年的老鸟,还是刚刚接触编程的小白,都会有时候清楚,有时候又有点模糊。因为,在实际应用中,它们之间分分离离,比如,敲代码都知道,x=3中x是变量,它不是参数,但是在函数y=3x+4中,x是变量,也是参数。那么什么这两个到底有什么区别和联系呢?我在网上搜了一下,发现很多说法,虽然大同小异,但是似乎只有下面这一段来自微软网站的比较高度抽象,而且意义涵盖深远。我摘抄过来,
局部变量 实例变量 类变量 全局变量 伪变量 常数 引用常数的优先顺序 您可以通过区分Ruby变量名的首位字符来确定它是局部变量、实例变量、类变量、全局变量还是常数。通常情况下,变量名的第二位字符以后是数字、字母或下划线,但有的内部变量名比较特殊,如“'$'+1个符号”(请参考内部变量)。变量名长度只受内存大小的限制。 局部变量 例: foobar 若标识符首位是小写字母或“_”,则该标识符就是
本文向大家介绍PHP 静态属性和变量,包括了PHP 静态属性和变量的使用技巧和注意事项,需要的朋友参考一下 示例 用public可见性定义的静态类属性在功能上与全局变量相同。可以从定义类的任何位置访问它们。 函数也可以在自己的范围内定义静态变量。这些静态变量通过多个函数调用持久化,这与在函数范围内定义的常规变量不同。这是实现Singleton设计模式的非常简单的方法:
主要内容:$_POST 变量,何时使用 method="post"?,PHP $_REQUEST 变量在 PHP 中,预定义的 $_POST 变量用于收集来自 method="post" 的表单中的值。 $_POST 变量 预定义的 $_POST 变量用于收集来自 method="post" 的表单中的值。 从带有 POST 方法的表单发送的信息,对任何人都是不可见的(不会显示在浏览器的地址栏),并且对发送信息的量也没有限制。 注释:然而,默认情况下,POST 方法的发送信息的量最大值为 8 M
主要内容:$_GET 变量,何时使用 method="get"?在 PHP 中,预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值。 $_GET 变量 预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值。 从带有 GET 方法的表单发送的信息,对任何人都是可见的(会显示在浏览器的地址栏),并且对发送信息的量也有限制。 实例 form.html 文件代码如下: 当用户点击 "Submit" 按钮时,发送到服