0

So right now i have an array named $socialMeta, containing:

Array (
    [0] => Array (
        [socialFacebook] => Array (
            [0] => http://www.facebook.com/someUsername
        )
    )
    [1] => Array (
        [socialYoutube] => Array (
            [0] => http://www.youtube.com/user/someUsername
        )
    )
    [2] => Array (
        [socialSoundcloud] => Array (
            [0] => http://www.soundcloud.com/someUsername
        )
    )
) 

From this array I need to create the following output:

<div class="social">
<a href="http://www.facebook.com/someUsername" class="fb" target="_blank">Add us on <span>Facebook</span></a>
<a href="http://www.youtube.com/user/someUsername" class="yt" target="_blank">Visit us on <span>Youtube</span></a>
<a href="http://www.soundcloud.com/someUsername" class="sc" target="_blank">Visit us on <span>Souncloud</span></a>
</div>

Please not that there are different anchor text for the first link.

For anchor classes i can use $socialMeta key to make whole process a bit easier.

3
  • It would be more effective to add an additional key called "network" or something similar with the name of the network (Youtube, Facebook, ...). Carrying such information in keys only makes this problem rather clunky since you will have to bruteforce your way forward. Can you add such a field in the arrays? Commented Aug 30, 2012 at 10:34
  • No, I can't. This array is created from Wordpress custom fields. Commented Aug 30, 2012 at 10:35
  • SORRY GUYS! I edited the QUESTION -- bad day for me ;) but improved my answer, too -- should match what he needs. Commented Aug 30, 2012 at 11:04

3 Answers 3

1
<?php if (!empty($socialMeta)) { ?>
<div class="social">
<?php foreach ($socialMeta as $rows) {?>
<?php foreach ($rows as $key => $val) {?>
<?php 
switch ($key) {
    case "socialFacebook":
        $title = "Facebook";
        $class = "fb";
        break;
    case "socialYoutube":
        $title = "Youtube";
        $class = "yt";
        break;
    case "socialSoundcloud":
        $title = "Souncloud";
        $class = "sc";
        break;
}
?>
<a href="<?php echo $val[0]; ?>" class="<?php echo $class; ?>" target="_blank">Add us on <span><?php echo $title; ?></span></a>
<?php }?>
<?php }?>
</div>
<?php }?>
Sign up to request clarification or add additional context in comments.

Comments

0

Start by identifying the network for each element in the array (I assume the name is $array in the following examples):

function add_network($array) {
  static $networks = array('Facebook', 'Youtube', 'Soundcloud');
  foreach($networks as $network)
    if (isset($array['social' . $network])) {
      $array['network'] = $network;
      return $array;
    }

  //None found
  $array['network'] = false;
  return $array;
}

$array = array_map('add_network', $array);

Then transform the array (you should find a better name for this function):

function transform_array($a) {
  static $classes = array('Youtube' => 'yt', 'Facebook' => 'fb', 'Soundcloud' => 'sc');
  $network = $a['network'];
  $class = $classes[$network];
  $url = $a['social' . $network][0]

  return array('network' => $network,
               'url' => $url,
               'class' => $class);
}

$array = array_map('transform_array', $array);

And now just loop over the elements of $array:

foreach($array as $row) {
  $network = $row['network'];
  $url = $row['url'];
  $class = $row['class'];
  if ($network === 'Facebook')
    $link_text = 'Add us on <span>%s</span>';
  else
    $link_text = 'Visit us on <span>%s</span>'

  $link_text = sprintf($link_text, $network);
  printf('<a href="%s" class="%s" target="_blank">%s</a>',
         $url, $class, $link_text);
}

Comments

0
<?php
function flattenArray(array $input){
    $nu = array();
    foreach($input as $k => $v){
        if(is_array($v) && count($v) == 1){
            $nu[key($v)] = current($v);
            if(is_array($nu[key($v)]) && count($v) == 1)
                    $nu[key($v)] = current($nu[key($v)]);
        }
        else
            $nu[$k] = $v;
    }

    return $nu;
}


// here you can maintain the sortorder of the output and add more social networks with the corresponding URL-text...
$urlData = array(
    'socialFacebook' => 'Add us on <span>Facebook></span>',
    'socialYoutube' => 'Visit us on <span>Youtube</span>',
    'socialSoundcloud' => 'Visit us on <span>Souncloud</span>',
);



$testArray = array(
    array('socialFacebook' => array('http.asdfsadf')),
    array('socialYoutube' => array('http.asdfsadf')),
    array('socialSoundcloud' => array('http.asdfsadf'))
);

$output = flattenArray($testArray);

HERE WE GO

echo '<div class="social">';
foreach($urlData as $network => $linkText){
    if(!empty($output[$network]))
        echo sprintf('<a href="%s" class="fb" target="_blank">%s</span></a>', $output[$network], $linkText);
}
echo '</div>';

1 Comment

yeah, for sure... you CAN do it in an other way... gimme a sec, i'll edit the post

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.