Php 如何获取数组元素的索引
我需要获取数组元素的索引,在我的例子中,Php 如何获取数组元素的索引,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我需要获取数组元素的索引,在我的例子中,$ch是数组元素,我需要索引值(例如:overview=array[0],$arval=0),这样我就可以打印$tabs[$arval+1] <?php $tab ='overview,gallery,video,songs$value1$value2$value3$value4'; $tabs = explode('$',$tab); $tabname = explode(',',$tabs[0]); echo '<div id="tab"
$ch
是数组元素,我需要索引值(例如:overview=array[0]
,$arval=0
),这样我就可以打印$tabs[$arval+1]
<?php
$tab ='overview,gallery,video,songs$value1$value2$value3$value4';
$tabs = explode('$',$tab);
$tabname = explode(',',$tabs[0]);
echo '<div id="tab" style="float:left;width:100%;height:30px;background:#333">';
foreach($tabname as $i)
{
echo '<a id="'.$i.'" style="color:#fff;padding:2px 10px;" href="?tab='.$i.'" >'.$i.'</a>';
}
echo '</div>';
if(isset($_GET['tab']))
{
$ch=$_GET['tab'];
foreach($tabname as $i){
if ($ch == $i)
// get the array index of the current element $arval
// echo $tabs[$arval+1]
} } ?>
我如何才能完成它?在您的
foreach
中,您需要执行以下操作:
foreach($tabname as $index => $i){
^^^^^^^^^
foreach($tabname as $index => $value){
// $index is the index
// $value is the value
if ($ch == $i)
// get the array index of the current element $arval
// echo $tabs[$arval+1]
}
在您的
foreach
中,您需要执行以下操作:
foreach($tabname as $index => $value){
// $index is the index
// $value is the value
if ($ch == $i)
// get the array index of the current element $arval
// echo $tabs[$arval+1]
}
也许这对你有用:
if(isset($_GET['tab']))
{
$ch=$_GET['tab'];
if($key = array_search($ch, $tabname, true))
// get the array index of the current element $arval
echo $tabs[$key];
}
}
也许这对你有用:
if(isset($_GET['tab']))
{
$ch=$_GET['tab'];
if($key = array_search($ch, $tabname, true))
// get the array index of the current element $arval
echo $tabs[$key];
}
}