将数组的单个元素写入fwrite文件

时间:2017-01-10 15:59:57

标签: php arrays

我尝试从MyArray函数返回一个数组,并使用[0]写入write2file并使用[1]填充pushover(myArray($jason))。我已经尝试了fwrite($myfile, echo $txt[0]);,但这失败了,只写了“数字'到文件而不是数组的内容。我看过print_r并且内爆,但没有运气。

write2file(myArray($json));

compare2files('old_version.txt', 'new_version.txt');
    if (compare2files('old_version.txt', 'new_version.txt') == 'false') {
    pushover(implode("\n",myArray($json)[1]));            
    rename("new_version.txt", "old_version.txt");
}

function write2file($txt) {
    $myfile = fopen("new_version.txt", "w") or die("Unable to open file!");
    fwrite($myfile, implode("\n",$txt[0]));
    fclose($myfile);
}

function compare2files($file1, $file2) {
    $file3 = file_get_contents($file1);
    $file4 = file_get_contents($file2);
    if (strcmp($file3, $file4) !== 0) {
        return 'false';
    }
}

function myArray($arry) {
    $selected = array(5,6,7,10,12,15,17,18,22,24);
    foreach($arry as $key => $value) {
        if (in_array($key, $selected) || empty($selected)) {
            $result .= $value['name']. chr(10);
            $xTime .= $value['name']. chr(10);
            $length = count($value["color"]);
            for ($i = 0; $i < $length; $i++) {
                if($value['time'][$i] != "") {

                    if(preg_match('/#ff0000/', $value['color'][$i])) {
                        $value['color'][$i] = 'RED';
                    }
                    if(preg_match('/#ffff00/', $value['color'][$i])) {
                        $value['color'][$i] = 'YELLOW';
                    }
                    if(preg_match('/#006600/', $value['color'][$i])) {
                        $value['color'][$i] = 'GREEN (Mini Disaster)';
                    }
                    if(preg_match('/#9933cc/', $value['color'][$i])) {
                        $value['color'][$i] = 'PURPLE (Trauma ByPass)';
                    }
                    if(preg_match('/#ff6600/', $value['color'][$i])) {
                        $value['color'][$i] = 'ORANGE (ReRoute)';
                    }

                    $xTime .= '  ' . $value["color"][$i] . ' - ' . $value["time"][$i] . chr(10);
                    $xColor .= '  ' . $value["color"][$i] . chr(10);
                    $results[0] = $xTime;
                    $results[1] = $xColor;
                }
            }
        }
    }
    return $results;
}

0 个答案:

没有答案