循环遍历foreach数组


Loop through foreach array

我在数组中循环时遇到问题,每次只得到一个值。我只想灰显1个值,因为我在jquery中调用该值。我是新来的,如果这太基本的话,我很抱歉,但我已经试了好几天了,把头发拔了出来!

$designslist = array('design1','design2','design3','design4');
$current_index = 0;
$current_id = current($designslist);
$next = $designslist[$current_index + 1];
foreach( $designslist as $value )
{
  if( $value !== $next )continue;
  $nexts =  $next;
  echo $nexts;
  $current_index++;
}

在我的html 内部

<a href="#" id="<?php echo $nexts; ?>">next</a>

使用jquery 调用id

$('#design1').on('click',function() {
  $('#web1').removeClass().addClass('big1');
});
foreach( $designslist as $value )

需要

foreach( $designslist as $i => $value )

$i是数组索引

您可以尝试数组搜索

$tag       = array_search($value, $designslist);
$nexts     = $$designslist[$tag]; 

好的,您似乎在使用这些"current_id"answers"next"变量混合for和foreach。

您可以使用这种foreach语句:

foreach ($designList as $key => $value) {
    echo "The key of $value is $key";
}

由于只想返回唯一的值,因此应该使用"array_unique"函数。试试这个:

foreach (array_unique($designList) as $key => $value) {
    echo "The key of $value is $key";
}

我知道这与这个问题无关,但是,正如你所说的,你是新手,你应该遵循一些PHP技巧。

您在变量中混合了snake_case和camelCase。我建议使用camelCase,但这取决于你。