我可以在同一个$ array上多次使用explode()吗?

时间:2019-06-03 12:02:03

标签: php

正如标题所述,我很好奇我是否可以在同一explode()上多次使用$array

我希望每次致电$array时都会覆盖explode()

如果我使用一次,它将填满$array。但是,如果$arraynot empty呢?

它将覆盖先前保存的元素吗?还是会在新的连续索引上添加新元素?

这是一种好习惯还是应该为每个explode()使用不同的数组?

我已经读过thisthis,但是找不到与我的问题有关的任何内容。

这是供参考的整个php页面:

<?php

$defaultFilePath = "./defaults.txt";
// chmod($inputsFilePath,0777);
$defaultSettingsFile = fopen($defaultFilePath, "r");

$inputsFilePath = "./inputs.txt";
$currentSettingsFile = fopen($inputsFilePath, "r");
$arrayInputs = [];

if($defaultSettingsFile && $currentSettingsFile.filesize()<=0) {
    echo "Reading from $defaultFilePath";
    // Fill $arrayInputs
    $arrayInputs = explode(PHP_EOL, fread($defaultSettingsFile, filesize($defaultFilePath)));
    fclose($defaultSettingsFile);
    // Output $arrayInputs
    var_dump($arrayInputs);
    $URL = $arrayInputs[0];
    $Timer = $arrayInputs[1];
    $Brightness = $arrayInputs[2];
}elseif($currentSettingsFile) {
    echo "Reading from $inputsFilePath";
    // Fill $arrayInputs
    $arrayInputs = explode(PHP_EOL, fread($currentSettingsFile, filesize($inputsFilePath)));
    $closeFlag = fclose($currentSettingsFile);
    // Output $arrayInputs
    var_dump($arrayInputs);
    $URL = $arrayInputs[0];
    $Timer = $arrayInputs[1];
    $Brightness = $arrayInputs[2];
}

//If Default Settings Button was pressed:
if(isset( $_POST['default_settings'])) {
    $defaultSettingsFile = fopen($defaultFilePath, "r");
    if($defaultSettingsFile) {
        echo "Reading from $defaultFilePath";
        // Fill $arrayInputs
        $arrayInputs = explode(PHP_EOL, fread($defaultSettingsFile, filesize($defaultFilePath)));
        fclose($defaultSettingsFile);
        // Output $arrayInputs
        var_dump($arrayInputs);
        $URL = $arrayInputs[0];
        $Timer = $arrayInputs[1];
        $Brightness = $arrayInputs[2];
    } else {
        echo "\nCouldn't change to Default Settings - defaults.txt couldn't be open !\n";
      }
}


// retrieve the form data by using the element's name attributes value as key
// If you press on "Save Values" button
if(isset( $_POST['save_values'])) {

    if(!empty($_POST['getURL'])) {
        $URL = $_POST['getURL'];
        $arrayInputs[0] = $URL;
    }

    if(!empty($_POST['getTimer'])) {
        $Timer = $_POST['getTimer'];
        $arrayInputs[1] = $Timer;
    }

    if(!empty($_POST['getBrightness'])) {
        $Brightness = $_POST['getBrightness'];
        $arrayInputs[2] = $Brightness;
    }
}

if(!$closeFlag) fclose($currentSettingsFile);

$currentSettingsFile = fopen($inputsFilePath, "c");
if($currentSettingsFile) {
    echo "Writing in $inputsFilePath";
    echo fwrite($currentSettingsFile,$arrayInputs[0].PHP_EOL);
    fwrite($currentSettingsFile,$arrayInputs[1].PHP_EOL);
    fwrite($currentSettingsFile,$arrayInputs[2].PHP_EOL);
    fclose($currentSettingsFile);
}

?>

1 个答案:

答案 0 :(得分:2)

这与explode()的行为无关,它仅返回一个数组。是您的分配每次都会覆盖变量。如果要附加连续爆炸的结果,则需要使用array_merge()。注释中建议的Union operator不适用于explode()的结果,因为它不是一个关联数组。

$str = '1-2-3';
$arr = explode('-', $str);
$str = '4-5-6';
$arr = array_merge($arr, explode('-', $str));
var_dump($arr);

输出:

array(6) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
  [3]=>
  string(1) "4"
  [4]=>
  string(1) "5"
  [5]=>
  string(1) "6"
}