2

I use the following code to get through each of values

foreach (array('facebook', 'twitter', 'vkontakte', 'mailru', 'odnoklassniki') as $service) {
    // Code goes here
}

But I feel there should be more beautiful solution than this one.

3
  • Nope, that's about as good as it gets. Commented Jun 6, 2011 at 17:39
  • It depends on what "code goes here." There are things like array_map that can be useful, but there's nothing wrong with what you've got. Commented Jun 6, 2011 at 17:42
  • 2
    it would be more beautiful without vkontakte and odnoklassniki =)) Commented Jun 6, 2011 at 17:56

3 Answers 3

4

Imo that's the most beatiful solution

If you need further information about loops you can go here

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

Comments

2

You could assign the array to a variable for readability and reuse.

$services = array('facebook', 'twitter', 'vkontakte', 'mailru', 'odnoklassniki');
foreach ( $services as $service) {
    // Code goes here
}

Depending on the situation, you could also use array_walk

array_walk($services, 'yourFunction');

function yourFunction(&$value, $key) {
    //Code goes here
}

Comments

0

what you have here fine as far as loops go is the right way to loop , but that does mean that you need a loop just depends on what //codes goes here is

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.