var_dump outputting string('**') “array”

2019-09-19 12:20发布

I am using a foreach loop and a var_dump but the var_dump from the following code outputs something strange. How do I get rid of the pre-prended sring() and quotation marks?

$dir = 'url/dir/dir/';    
$images_array = glob($dir.'*.jpg'); 

$images = array();

foreach ($images_array as $image) {
    $images[] = str_replace($dir, '', $image);   
}


var_dump(implode(',', $images)); 

Output:

string(51) "image1.jpg,image2.jpg,image3.jpg,image4.jpg"

3条回答
迷人小祖宗
2楼-- · 2019-09-19 13:00

var_dump isn't outputting anything 'strange'. That is what it's supposed to do. It's for debugging, not for echoing.

Just echo the string you want:

echo implode(',', $images);
查看更多
趁早两清
3楼-- · 2019-09-19 13:05

That's what var_dump does - it prints the datatype and the length. If you want to output just the string use

echo implode(',', $images);
查看更多
Rolldiameter
4楼-- · 2019-09-19 13:15

var_dump returns you type of variable and all information about it. If you use it with HTML <pre>

echo '<pre>';
var_dump($images);

it will print for you an array with all elements in new lines.

If:

echo '<pre>';
var_dump(implode(',', $images)); 

it returns string. And also shows you that it is a string.

If you just want to print value, use echo:

echo implode(',', $images); 
查看更多
登录 后发表回答