0

I am looking for a way to display social media icons on my website. My icons alternate colors depending on the social media service.

  1. Priority number 1 - Sort the array items by alternating colors.
  2. Priority number 2 - Sort by color groups.

I would like it if it could have two conditional checks.

Firstly attempting to sort by alternating color. However, there may be three silver icons and only 1 blue icon.

Secondly, if alternating the colors didn't work. I would then prefer if it would just clump the icons by color instead. So I might have 1 blue icon first, following with 3 silver icons after.

Here is my array of icon data:

Array
(
    [0] => Array
        (
            [service] => twitter
            [url] => https://twitter.com/1/
            [color] => silver
        )

    [1] => Array
        (
            [service] => google_plus
            [url] => https://plus.google.com/2/posts
            [color] => silver
        )

    [2] => Array
        (
            [service] => facebook
            [url] => https://www.facebook.com/3/
            [color] => blue
        )

    [3] => Array
        (
            [service] => linkedin
            [url] => http://us.linkedin.com/company/4/
            [color] => blue
        )

    [4] => Array
        (
            [service] => youtube
            [url] => http://www.youtube.com/user/5/
            [color] => silver
        )

)

I am not necessarily looking for a full fledged code solution to my question. More or less just looking for advice on how I can achieve this or what functions I should use.

3
  • so how do you decide if priority #1 can't be met, and fall back to priority #2? Commented Nov 30, 2012 at 22:26
  • 1
    Are there only two possible colors? Commented Nov 30, 2012 at 22:28
  • @fiskfisk Yes, only two colors. Commented Nov 30, 2012 at 22:29

2 Answers 2

2

Create a couple new empty arrays, named the colors you are looking for. foreach through the original array and enter the original array's index in the proper array (if a silver icon, enter in the $silver array).

You should end up with something like:

$silver = array(0, 1, 4);
$blue = array(2, 3);

Once you have those new arrays, count them, if one is more than 1 digit away from the other, alternating won't work. Just output the indexes from one array before the other.

If the icons can alternate, just output an entry from the larger one, then the other, and alternate until both are empty.

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

Comments

1

You can try

$silver = array_filter($data,function($v){ return $v['color'] == "silver" ;});
$blue = array_filter($data,function($v){ return $v['color'] == "blue" ;});

foreach(array_map(null, $silver,$blue) as $set)
{
    foreach($set as $var)
    {
        printf("<a href=\"%s\" style=\"color:%s;\">%s</a> <br />",$var['url'],$var['color'],$var['service']);
    }
}

2 Comments

Interesting approach. What's up with the function definitions inside of another PHP function? I didn't even know that was possible. Is there documentation available on this type of usage?
Its called Anonymous functions or closures see php.net/manual/en/functions.anonymous.php

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.