mafortis
mafortis

Reputation: 7128

Getting array out of something in php

I don't even know what should I call this type of data but I need to get array out of it.

Code

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

DD result

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

What I need

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

any idea?

Update

I changed my code to:

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

Now I'm getting:

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

I have 1 extra row there, how to remove it?

Update 2

If I use

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

same result as using preg_split happens (extra line)

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

Upvotes: 0

Views: 61

Answers (3)

zainul ogna
zainul ogna

Reputation: 190

Try this to remove empty values from array

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

Upvotes: 1

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

You can use array_filter to remove the empty values from an array

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

Upvotes: 0

Imran
Imran

Reputation: 4750

Replace this line:

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

by:

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

Upvotes: 0

Related Questions