0

I have an array in the format

Array
(
    [/Callum/] => Array
        (
            [0] => ##chan1
        )
    [/Adam/] => Array
        (
            [0] => ##chan2
        )   
    [/Chris)/] => Array
        (
            [0] => ##chan1
        )
    [/Mike*/] => Array
        (
            [0] => ##chan3
        )
)

And from this I use the below code to try and get the id of the array that each channel features in.

foreach($array as $row)
{

    if (in_array($buf['channel'],$row))
    {
        $return = $return." ".current(array_keys($array,$row));
    }
}

My problem is that current() doesnt seem to work the way I am expecting it to. Currently if the $buf /Callum/ twice rather than /Callum/ and /Chris/

2
  • array_keys is going to return a NEW array each time, which will always have its internal pointer set to the FIRST element. Commented Mar 15, 2012 at 17:14
  • ahh, definatly explains why that wasn't working Commented Mar 15, 2012 at 17:20

2 Answers 2

2

Why not:

foreach($array as $key => $row)
{

    if (in_array($buf['channel'],$row))
    {
        $return = $return . " " . $key;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this instead

foreach($array is $id => $row){
    $return .=" ".$id;
}

edit:

foreach($array is $id => $row){
    if($row[0] == $buf['channel']){
        echo $key; //This is your key
    }

}

2 Comments

Is this to replace the entire foreach or just the internal if?
The whole thing. But remove the $return and add your if-statement again, I think I missunderstood you..i'll edit my ans..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.