数组到字符串转换
问题描述:
在这段代码中,我试图从数组中显示图像,但是,我得到的数组是字符串转换错误。从我GOOGLE了,你将不得不使用print_r
为了打印字符串,但我不完全知道我怎么能做到这一点:数组到字符串转换
<?php
$item_array = array(
1 => array('product_img' => "images/013_8c9f517a.jpg",
'product_name' => "black tshirt",
'product_price' => "€99.00"),
2 => array('product_img' => "images/014_28ded7a4.jpg",
'product_name' => "white tshirt",
'product_price' => "€99.00"));
echo "<img src=$item_array[1]['product_img']>";
?>
答
你只需要使用适当的字符串连接。改变这一行:
echo "<img src=$item_array[1]['product_img']>";
向其中一个选项:
echo "<img src={$item_array[1]['product_img']}>";
echo "<img src=".$item_array[1]['product_img'].">";
希望它能帮助。
答
提取一个临时变量可能使事情变得很简单:
$src = $item_array[1]['product_img'];
echo "<img src=${src}>";