Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组的单个元素写入fwrite文件_Php_Arrays - Fatal编程技术网

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

Php 将数组的单个元素写入fwrite文件,php,arrays,Php,Arrays,我试图从MyArray函数返回一个数组,并使用[0]写入2个文件,使用[1]填充pushover(MyArray($jason))。我尝试了fwrite($myfile,echo$txt[0])但此操作失败,只将单词“Array”写入文件,而不是数组的内容。我看过印刷品和内爆,但运气不好 write2file(myArray($json)); compare2files('old_version.txt', 'new_version.txt'); if (compare2files('

我试图从
MyArray
函数返回一个数组,并使用[0]写入2个文件,使用[1]填充
pushover(MyArray($jason))
。我尝试了
fwrite($myfile,echo$txt[0])
但此操作失败,只将单词“Array”写入文件,而不是数组的内容。我看过印刷品和内爆,但运气不好

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;
}
write2file(myArray($json));
比较2个文件('old_version.txt'、'new_version.txt');
if(compare2files('old_version.txt','new_version.txt')=='false'){
pushover(内爆(“\n”,myArray($json)[1]);
重命名(“new_version.txt”、“old_version.txt”);
}
函数write2file($txt){
$myfile=fopen(“new_version.txt”,“w”)或die(“无法打开文件!”);
fwrite($myfile,内爆(“\n,$txt[0]);
fclose($myfile);
}
函数compare2files($file1,$file2){
$file3=文件获取内容($file1);
$file4=文件获取内容($file2);
if(strcmp($file3,$file4)!==0){
返回“false”;
}
}
函数myArray($arry){
$selected=数组(5,6,7,10,12,15,17,18,22,24);
foreach($arry as$key=>$value){
if(在_数组中($key,$selected)| |空($selected)){
$result.=$value['name'].chr(10);
$xTime.=$value['name'].chr(10);
$length=计数($value[“color”]);
对于($i=0;$i<$length;$i++){
如果($value['time'][$i]!=“”){
if(preg#u匹配('/#ff0000/',$value['color'][$i])){
$value['color'][$i]='RED';
}
if(preg#u匹配('/#ffff00/',$value['color'][$i])){
$value['color'][$i]='YELLOW';
}
if(preg#u匹配('/#006600/',$value['color'][$i])){
$value['color'][$i]='GREEN(迷你灾难)';
}
if(preg#u匹配('/#9933cc/',$value['color'][$i])){
$value['color'][$i]='PURPLE(创伤旁路)';
}
if(preg#u匹配('/#ff6600/',$value['color'][$i])){
$value['color'][$i]='ORANGE(重新路由)';
}
$xTime.='.$value[“color”][$i].-'.$value[“time”][$i].chr(10);
$xColor.=''.$value[“color”][$i].chr(10);
$results[0]=$xTime;
$results[1]=$xColor;
}
}
}
}
返回$results;
}

echo
对数组中的一个元素进行加密并获得“array”作为输出,这表明您拥有一个多维数组
fwrite
需要一个字符串。我也尝试过序列化,但运气不好,我遗漏了什么?视情况而定。a)
myArray()
函数的
$arry
输入是什么?是否要将
$txt[0]
的内容分别输入到文件中?如果是这样的话<代码>写入($myfile,内爆(“\n”,$txt[0])是内爆不工作-内爆行中给出无效参数。