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

馋奶兔 提交于 2019-12-13 08:29:49

问题


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


来源:https://stackoverflow.com/questions/5194996/var-dump-outputting-string-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!