PHP - 如何使用给定数组格式化此输出?

时间:2012-08-30 10:31:49

标签: php arrays

所以现在我有一个名为$ 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键使整个过程更容易。

3 个答案:

答案 0 :(得分: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 }?>

答案 1 :(得分: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>';

答案 2 :(得分: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);
}