【问题标题】:How can I display an array in a human readable format?如何以人类可读的格式显示数组?
【发布时间】:2009-06-18 04:54:56
【问题描述】:

如果我有一个如下所示的数组:

$str = '';
if( $_POST['first'] )
    $str = $_POST['first'];
if( $_POST['second'] )
    $str .= ($str != '' ? ',' : '') . $_POST['second'];
if( $_POST['third'] )
    $str .= ($str != '' ? ',' : '') . $_POST['third'];
if( $_POST['fourth'] )
    $str .= ($str != '' ? ',' : '') . $_POST['second'];
$str .= ($str != '' ? '.' : '');

这给了我这样的东西:

乔、亚当、迈克。

但是,我想在最后一项之前添加一个“and”。

所以它会这样写:

乔、亚当、迈克。

如何修改我的代码来做到这一点?

【问题讨论】:

    标签: php arrays formatting


    【解决方案1】:

    数组非常适合这个:

    $str = array();
    foreach (array('first','second','third','fourth') as $k) {
        if (isset($_POST[$k]) && $_POST[$k]) {
            $str[] = $_POST[$k];
        }
    }
    $last = array_pop($str);
    echo implode(", ", $str) . " and " . $last;
    

    当有一个项目时,您可能应该对上述情况进行特殊处理。事实上,我编写了一个名为“conjunction”的函数,它完成了上述操作,并包含了特殊情况:

    function conjunction($x, $c="or")
    {
        if (count($x) <= 1) {
            return implode("", $x);
        }
        $ll = array_pop($x);
        return implode(", ", $x) . " $c $ll";
    }
    

    好问题!

    更新:通用方法:

    function and_form_fields($fields)
    {
         $str = array();
         foreach ($fields as $k) {
             if (array_key_exists($k, $_POST) && $v = trim($_POST[$k])) {
                  $str[] = $v;
             }
         }
         return conjunction($str, "and");
    }
    
    ...
    
    and_form_fields(array("Name_1","Name_2",...));
    

    我添加了正确的 $_POST 检查以避免通知和空白值。

    【讨论】:

    • 加上你的函数定义押韵:P
    • 上面只返回:第一、第二、第三、第四我要加上最后一个逗号,通常称为连字符。我怎样才能修改上面的数组来完成这个?谢谢大家!
    • 我认为连续逗号在技术上是不正确的英文,但是,只需将最后一行更改为: ", and " 。 $last --OR-- ", $c $ll"; B
    • 再次感谢!我很抱歉我的所有问题.. 所以我有一个带有文本字段 (5) 的表单,根据用户是否输入某些内容,这些表单会被带到我想要写出的第二页(只有 1 个或任何组合)使用上述功能。如果我的文本字段表单被称为“Name_1”“Name_2”等。我将如何使用该函数来请求这些表单项?我再次感谢你!!我以前从未玩过函数,现在我明白你所说的“特殊情况”是什么意思了。
    【解决方案2】:

    我想到的第一个想法是始终在辅助变量中保留姓氏。当你有另一个要输入的时候,你把它加上“逗号”并在辅助中获得下一个名字。

    最后,当您添加完名称后,添加“and”和来自 aux 的姓氏。

    【讨论】:

      【解决方案3】:

      为什么不将它们作为一个单独的变量发布,而不是将它们发布为一个数组:

      #pull the array from the POST:
      $postedarray = $_POST['names'];
      
      #count the number of elements in the posted array:
      $numinarray = count($postedarray);
      
      #subtract 1 from the number of elements, because array elements start at 0
      $numinarray = $numinarray -1;
      
      #set a prepend string
      $prependstr = "and ";
      
      #Pull the last element of the array
      $lastname = $postedarray[$numinarray];
      
      #re-define the last element to contan the prepended string
      $postedarray[$numinarray] = "$prependstr$lastname";
      
      #format the array for your requested output
      $comma_separated = implode(", ", $postedarray);
      
      print "$comma_separated";
      

      【讨论】:

        猜你喜欢
        • 2014-02-24
        • 2016-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-30
        • 2011-07-20
        • 2011-12-25
        • 2018-04-18
        相关资源
        最近更新 更多