smc Posted May 13, 2007 Share Posted May 13, 2007 Hey everyone, I have a script that checks an array for names and if that person's name is there it will e-mail them a particular variable. Here is the setup: an array such as $names = array( $name1, $name2, $name3, $name4 ); etc. Then a foreach loop foreach ( $names as $person ){ if ( $key == 1 ){ $color = blue; } } However I need something that will get the position in the array of the variable that the foreach loop is dealing with. I tried array_search but that gets screwed up if someone is in the array more than once, it will then only stop at the first time they are in the away. I would appreciate any help you can offer! Quote Link to comment https://forums.phpfreaks.com/topic/51204-solved-position-in-array-in-a-foreach-loop/ Share on other sites More sharing options...
chigley Posted May 13, 2007 Share Posted May 13, 2007 If the array values are stored like this: 0 => name1 1 => name2 Then you can access the 0 or 1 value like this: <?php foreach($names as $pos => $person) { ... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51204-solved-position-in-array-in-a-foreach-loop/#findComment-252125 Share on other sites More sharing options...
smc Posted May 13, 2007 Author Share Posted May 13, 2007 Well this is the current setup of the array: $anchors = array( $m_anchor1, $m_anchor2, $t_anchor1, $t_anchor2, $w_anchor1, $w_anchor2, $th_anchor1, $th_anchor2, $f_anchor1, $f_anchor2, $m_director, $t_director, $w_director, $th_director, $f_director, $m_weather, $f_weather, $t_sports, $f_sports ); If I add the 0 => $m_anchor1, 1 => $m_anchor2 will it work? Or will it still work as is? Quote Link to comment https://forums.phpfreaks.com/topic/51204-solved-position-in-array-in-a-foreach-loop/#findComment-252130 Share on other sites More sharing options...
chigley Posted May 13, 2007 Share Posted May 13, 2007 As far as I know there isn't a way to get the key if they haven't been defined by the user. I'd define the keys yourself Quote Link to comment https://forums.phpfreaks.com/topic/51204-solved-position-in-array-in-a-foreach-loop/#findComment-252136 Share on other sites More sharing options...
kenrbnsn Posted May 13, 2007 Share Posted May 13, 2007 By default, an array has numerical indices, you can see this by doing either <?php $anchors = array( $m_anchor1, $m_anchor2, $t_anchor1, $t_anchor2, $w_anchor1, $w_anchor2, $th_anchor1, $th_anchor2, $f_anchor1, $f_anchor2, $m_director, $t_director, $w_director, $th_director, $f_director, $m_weather, $f_weather, $t_sports, $f_sports ); echo '<pre>' . print_r($anchors,true) . '</pre>'; ?> or <?php $anchors = array( $m_anchor1, $m_anchor2, $t_anchor1, $t_anchor2, $w_anchor1, $w_anchor2, $th_anchor1, $th_anchor2, $f_anchor1, $f_anchor2, $m_director, $t_director, $w_director, $th_director, $f_director, $m_weather, $f_weather, $t_sports, $f_sports ); foreach ($anchors as $i => $v) echo $i . ' := "' . $v . "<br>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/51204-solved-position-in-array-in-a-foreach-loop/#findComment-252140 Share on other sites More sharing options...
smc Posted May 13, 2007 Author Share Posted May 13, 2007 Thanks chigley, that did it! Also thank you Ken, I've saved that for later use! Quote Link to comment https://forums.phpfreaks.com/topic/51204-solved-position-in-array-in-a-foreach-loop/#findComment-252143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.