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

2019-09-19 12:51发布

问题:

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"

回答1:

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);


回答2:

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:

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);