0

所以现在我有一个名为 $socialMeta 的数组,其中包含:

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
        )
    )
) 

从这个数组中,我需要创建以下输出:

<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>

请注意,第一个链接有不同的锚文本。

对于锚类,我可以使用 $socialMeta 键使整个过程更容易一些。

4

3 回答 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 }?>
于 2012-08-30T10:49:35.157 回答
0

首先为数组中的每个元素识别网络(我假设名称$array在以下示例中):

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);

然后转换数组(你应该为这个函数找到一个更好的名字):

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);

现在只需遍历以下元素$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);
}
于 2012-08-30T10:49:11.390 回答
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);

开始了

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>';
于 2012-08-30T10:46:36.423 回答