Jump to content

Recommended Posts

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!

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.