[英]How to get in php foreach value from other array?
I get images in a array then i read it with a foreach this are images, but how can i get also image text, my example is here: 我得到数组中的图像,然后用foreach读取它,这是图像,但是如何获取图像文本,我的示例在这里:
$slideImages = array('image/path/image1', 'image/path/image2', 'image/path/image13');
$slideText = array('image text 1', 'image text 2', 'image text 3');
if($slideImages) :
echo '<ul>';
foreach($slideImages as $slideImage) :
?>
<li>
<div>IMAGE TEXT</div>
<img src="<?php echo $slideImage; ?>" />
</li>
<?php
endforeach;
echo '</ul>';
endif;
I need to embed also image text from the true position, i know how to do it with for but so is not efficient... it's something like this possible? 我还需要从真实位置嵌入图像文本,我知道该怎么做,但是效率不高……可能是这样吗?
Assuming the keys are the same, use the $key
in the foreach: 假设密钥相同,请在foreach中使用
$key
:
foreach($slideImages as $key => $slideImage) :
?>
<li>
<div>IMAGE TEXT</div>
<img src="<?php echo $slideImage; ?>" />
<?php echo $slideText[$key]; ?>
</li>
<?php
endforeach;
You could also change the way you store the info and keep them in one multidimensional array. 您还可以更改存储信息的方式,并将其保存在一个多维数组中。
$slides = array(
array( image => "image/path/image1", text => "image text 1"),
array( image => "image/path/image2", text => "image text 2"),
array( image => "image/path/image3", text => "image text 3")
);
Then you would iterate through the $slides array and just pull out the image and text for each: 然后,您将遍历$ slides数组,并仅提取每个图像和文本:
foreach($slides as $slide)
{
echo "Text:".$slide["text"];
echo "Image Path:".$slide["image"];
}
This organizes your code a little better so you don't wind up with something crazy going on should your two arrays wind up out of sync. 这样可以更好地组织代码,因此,如果两个阵列不同步,就不会发生疯狂的事情。
simplified example: 简化示例:
foreach($slideImages as $key=>$var){
echo $var;
echo $slideText[$key];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.