PHP 和带数组的变量变量

php and variable variables with array

提问人:user756659 提问时间:3/22/2014 最后编辑:user756659 更新时间:3/23/2014 访问量:281

问:

尝试最终得到 7 个数组($mon、$tue、$wed、$thu、$fri、$sat、$sun),这些数组在给定初始数组$hours的一天中每个小时都包含 0 或 1 的值。$hours只是一个已发布的数组,其中包含一周中每天的用户选择小时数。

我相信问题在于我使用了变量($ $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;
        }
    }           
}
PHP 数组 变量

评论

2赞 nice ass 3/22/2014
${$day}[...]而不是 ,因为 PHP 无法确定您希望变量的名称是 或$$day[..]$day$day[..]
1赞 IMSoP 3/22/2014
“我相信问题在于我对变量的使用”——好吧,在我看来,使用变量总是一个问题。为什么你不使用关联数组来代替,就像你使用for一样?$hours
0赞 user756659 3/22/2014
@onetrickpony - 完美...这正在按预期工作,现在我看到它是有道理的。
0赞 IMSoP 3/22/2014
其他提高可读性的东西: in_array() 返回布尔值,所以你不需要它;这 24 行非常相似的代码应该变成一个循环,最后 7 个调用也应该变成一个循环(如果你有一个关联数组,很容易:你可以只使用 );如果您可以控制数据格式,请使用 24 小时制,而不是所有那些略微不兼容的小时标签。哦,这不是一个好的保存格式,因为它不是为作为数据读回而设计的;try 或 ,或者也许== 1file_put_contentsforeachprint_rvar_exportserializejson_encode
0赞 user756659 3/23/2014
@IMSoP - 你在想什么?$hours可以是空的,也可以只包含几天的值,当然也可以是全部。我能想到的每一种方法仍然需要一种方法来了解实际的一天。

答:

1赞 nice ass 3/23/2014 #1

我会像这样重组该代码:

// 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));
}

如果要保持键名之间的不一致,请使用 onsubstr($def, 0, -1)$output

评论

0赞 IMSoP 3/23/2014
这很整洁,但我有几个吹毛求疵的地方:我认为这将消除 OP 想要的数据,但我可能是错的。像这样跳过最里面的通常被认为是糟糕的编码风格,因为通过在下面添加一行而没有意识到它不在循环中来引入错误是非常容易的。continue{}foreach