6

I have the following code.. and I know it's probably all wrong, but I haven't dealt with foreach loops before.

$last_names = regapiGetLastNames( NULL, -1 );
foreach ($last_names as $name => $last_name_id)
    $exclude = array('11196','11195','11198','11197');
    if(!in_array($name->last_name_id, $exclude)):
    print '<option value="'.$last_name_id.'">'.$name.'</option>';

Obviously its going wrong somewhere, any help pls?

1
  • put this on your page and post the results here. $last_names = regapiGetLastNames( NULL, -1 ); print_r($last_names); Commented Nov 17, 2011 at 22:50

4 Answers 4

4

If the IDs are array values, then you can also use array_diff to filter them:

$last_names = regapiGetLastNames( NULL, -1 );

$exclude = array('11196','11195','11198','11197');
$last_names = array_diff($last_names, $exclude);

foreach ($last_names as $name => $last_name_id) {
    print '<option value="'.$last_name_id.'">'.$name.'</option>';
}
Sign up to request clarification or add additional context in comments.

Comments

2
$last_names = regapiGetLastNames( NULL, -1 );
$exclude = array('11196','11195','11198','11197');
foreach ($last_names as $name => $last_name_id)
{
    if(!in_array($name->last_name_id, $exclude))
        print '<option value="'.$last_name_id.'">'.$name.'</option>';
}

You need the braces for a multiline loop. also, move the array declaration outside the loop

4 Comments

This solution looks solid, although single line ifs without {} is bad practice imo.
also, $name => $last_name_id looks like you can just call $last_name_id, but since you used $name->last_name_id I assumed you intended it this way. Maybe $name is an object. I don't know.
yeah you can debate any non{} and in the end it is always bad practice I think. Just I wanted to show how he WOULD use it since his original had no braces.
This doesn't exclude the names.. they still show up in the select.
0

You could turn that into...

$last_names = regapiGetLastNames( NULL, -1 );

$last_names = array_filter($last_names, function($value) {
    return in_array($value, array('11196','11195','11198','11197'));
});

foreach ($last_names as $name => $last_name_id) {
    print '<option value="'.$last_name_id.'">'.$name.'</option>';
}

When you get to doing your loop, you are only iterating over the set you want. This is useful for separating your business rules (dropping certain ids) and presentation (echoing the HTML).

Comments

0

I'd probably write it a little different this isn't correct ($name isn't an object)

$name->last_name_id

Something along the lines of:

$last_names = regapiGetLastNames( NULL, -1 );
$exclude = array('11196','11195','11198','11197');

foreach ($last_names as $name => $last_name_id) {
    if(!in_array($last_name_id, $exclude)) print '<option value="'.$last_name_id.'">'.$name.'</option>';
}

Comments

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.