[英]adding arrays inside foreach
I'm having issues getting arrays to stick within a foreach loop. 我在使数组粘在foreach循环中遇到问题。
$sites = array($siteOne, $siteTwo);
$tags = array('siteURL' => '', 'name' => '');
foreach($sites as $value){
$value = $tags;
$value['siteURL'] = 'one';
$value['name'] = 'two';
}
print_r($sites);
echo '<br>';
print_r($sites[$siteOne]);
echo '<br>';
the few lines were to get output, but I'm only getting the first $sites array to show 几行是要输出的,但是我只得到第一个$ sites数组来显示
my thoughts were that $value=$tags would add that array to each value in the $sites array maybe I'm not understanding foreach loops correctly? 我的想法是$ value = $ tags会将那个数组添加到$ sites数组中的每个值,也许我不是正确理解了foreach循环?
thanks 谢谢
try like this 这样尝试
$sites = array('saiyam', 'sandip');
$tags = array('siteURL' => 'testurl', 'name' => 'testname');
$newarr = array();
foreach($sites as $value){
foreach ($tags as $k => $tag){
$newarr[$value][$k] = $tag;
}
}
echo '<br>';
print_r($newarr);
echo '<br>';
May your need... cheers :) 可能你的需要...加油:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.