0

I have this code: ids row is like ( 54,88,15,78) a string concat of id:

$sql = "SELECT *, ids FROM.....";
$results = $Database->Select($sql);
$last = end($results);
foreach($results as $i){
    $userArray = explode(",",$i['ids']);
    if( in_array($_SESSION['AUTH_ID'], $userArray) ){
        echo $i['name'];
        if($last != $i)
            echo ",\n";
    }
}

This prints always a comma after $i['name']... this is wrong.. how can i fix it?? How can I count in_array new value??

1 Answer 1

7

There is an easier way to do this:

/* ... */
$logged_in[] = array();
if( in_array($_SESSION['AUTH_ID'], $userArray) ){
    $logged_in[] = $i['name'];
}

echo implode(', ', $logged_in);

This prints Alice, Bob

Sign up to request clarification or add additional context in comments.

1 Comment

Use implode instead of join otherwise, yes. (It's a very rarely used alias of implode and the code already uses explode.)

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.