PHP移动数组项显示正确的数组但写错了吗? [英] PHP Moving Array Item Shows Correct Array but Writes Wrong?

查看:70
本文介绍了PHP移动数组项显示正确的数组但写错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有将数组元素上移一个空格的代码。

URL加载:
action?p = ArrayNumber

Have a code that shifts an element of an array up one space.
URL loads: action?p=ArrayNumber

if (isset($_GET['p'])) {
    $index = $_GET['p'];
    $panel_dir = 'host.txt';
    $panel_data = file($panel_dir);

    $pos = $panel_data[$index];
    $panel_data[$index] = $panel_data[$index-1];
    $panel_data[$index-1] = $pos;

    $f_panel = fopen($panel_dir, "w+");
    foreach($panel_data as $panel_line) {
        fwrite($f_panel, $panel_line);
    }
    fclose($f_panel);  
}

内容如何开始:

Array ( [0] => Name [1] => List [2] => Folder [3] => Host )

当print_r($ panel_data);正确显示的数组:

When print_r($panel_data); the array it shows up correctly:

Array ( [0] => Name [1] => List [2] => Host [3] => Folder )

当echo implode($ panel_data);数组结合了移动的元素:

When echo implode($panel_data); the array combines the moved element:

Name List HostFolder

this(?!)的Bcz,似乎将它们组合而不是换行。它确实会按需要移动它,但是..不知道合并的来源。

Bcz of this(?!), it seems to write them combined instead of a new line. It does move it as desire but .. no idea where the combining is coming from.

Name  
List  
HostFolder


推荐答案

使用内爆( \n,$ array); 将行写入到您的文件中。

Use implode("\n", $array); to write lines in to you file.

以下是工作示例:

    $array = [
        'Name',
        'List',
        'Folder',
        'Host'
    ];
    $host = $array[3];
    $array[3] = $array[3-1];
    $array[3-1] = $host;
    echo implode("\n", $array);

结果:

Name
List
Host
Folder

这篇关于PHP移动数组项显示正确的数组但写错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆