php和变量与数组

时间:2014-03-22 15:37:06

标签: php arrays variable-variables

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

我认为问题是我使用变量变量($$天),因为我的工作日数组都返回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;
        }
    }           
}

1 个答案:

答案 0 :(得分: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));
}

如果您想保持关键名称之间的不一致,请使用substr($def, 0, -1)上的$output