在PHP中获取数组

时间:2019-04-14 17:29:07

标签: php laravel

我什至不知道我应该怎么称呼这种类型的数据,但我需要从中取出数组。

代码

$folderss = $ssh->exec($command);
$folders = explode(',', $folderss);

DD结果

array:1 [▼
  0 => """
    DO_NOT_UPLOAD_HERE\n
    domains\n
    public_html\n
    """
]

我需要什么

array:3 [▼
      0 => "DO_NOT_UPLOAD_HERE",
      1 =>  "domains",
      2 => "public_html",
]

有什么想法吗?

更新

我将代码更改为:

$folders = preg_split("/[\s,]+/", $folderss);

现在我得到:

array:4 [▼
  0 => "DO_NOT_UPLOAD_HERE"
  1 => "domains"
  2 => "public_html"
  3 => ""
]

我那里还有1行,如何将其删除?

更新2

如果我使用

$folders = explode("\n", $folderss);

发生与使用preg_split相同的结果(多行)

array:4 [▼
  0 => "DO_NOT_UPLOAD_HERE"
  1 => "domains"
  2 => "public_html"
  3 => ""
]

3 个答案:

答案 0 :(得分:1)

尝试此操作从数组中删除空值

 $folders = preg_split("/[\s,]+/", $folderss); 
 $folders=array_filter($folders);

答案 1 :(得分:0)

替换此行:

$folders = explode(',', $folderss);

作者:

$folders = explode('\n', trim($folderss, '"');

答案 2 :(得分:0)

您可以使用array_filterarray删除空值

$folders = array_filter(preg_split("/[\s,]+/", $folderss));