【问题标题】:Return unordered list contents from function PHP从函数 PHP 返回无序列表内容
【发布时间】:2012-04-13 04:37:18
【问题描述】:

我非常感谢任何人在这方面的帮助。基本上我想制作一个由其他数组组成的列表数组,以绕过 wordpress 的 do_shortcode 函数的限制。我正在使用许多函数来执行此操作。

问题的详细版本:

目前的代码如下所示:

    /* These are the functions contained in a functions file */

    function output_entry_data() {
   $postdate = get_the_date('j/n/y');
   $entrytitle = get_the_title();

return ('<li><p class="postmeta">'. $postdate. '</p><h3>'. $entrytitle. '</h3></li>');
    }



    function output_month_data($entrynomax = '', $month = '', $entrydata = '') {
   $entryno = 1;

   while($entryno <= $entrynomax) {
    echo $entrydata[$entryno];
    $entryno++;
   }

    }


    function output_year_data($monthlynomax = '', $year = '', $monthlydata = '') {
   $monthno = 1;

   while($monthno <= $monthnomax) {
    echo do_shortcode('<h4>[slider title="'. $month. '"]</h4><ul>'. $monthlydata[$monthno]. '</ul>[/slider]');
    $monthno++;
   }

    }

    /* This is from a loop that determines whether you have reached the end of a month or a year */

    $entrydata[$entryno] = output_entry_data();
    $entrynomax = $entryno;

    $monthlydata = array($monthno => $monthno);
    $monthlydata[$monthno] = return(output_month_data($entrynomax, $month, $entrydata));
    $monthlynomax = $monthno;

    $annualdata[$yearno] = array($yearno => $yearno);
    $annualdata[$yearno] = return(output_year_data($monthlynomax, $year, $monthlydata));

    $entryno = 1;
    $monthno = 1;
    $yearno++;
    $yearo = get_the_date('Y');

    /* The idea is that all the data gets outputted at the end of the loop like this: */

    $yearnomax = $yearno;

    echo ('<ul>');

   $yearno = 1;

   if($yearno <= $yearnomax) {
    echo do_shortcode('<h3>[expand title ="'. $year. '"]</h3><ul>'. $annualdata[$yearno]. '</ul>[/expand]');
    $yearno++;
   }

    echo('</ul>');

目前代码正在成功创建 $entrydata[$entryno] 数组,因为函数 output_entry_data() 每次只返回一行代码。

但是,当我尝试为每个月创建数组 $monthlydata[$monthno] 时,它只是运行函数 output_month_data() 并列出所有每月条目,而不是将数据传递给数组被其他函数使用。

我可以看到这是因为我在 output_entry_data() 中使用了“return”,在 output_month_data() 中使用了“echo”

问题的简短版本

数组 $entrydata[$entryno] 中的每个项目都是一个包含列表项标签的字符串,我希望 output_monthly_data() 返回一个大字符串,其中包含 $entrydata[$entryno] 中的所有项目以供其他函数使用,而不是像当前的代码那样回显它们。当涉及到一个while循环时可以这样做吗?

非常感谢,如有任何意见,我将不胜感激。

【问题讨论】:

    标签: php arrays list return unordered


    【解决方案1】:

    是的,这(很容易)是可能的。至少有两种方式

    1. 连接字符串并返回结果字符串:

      function output_month_data($entrynomax = '', $month = '', $entrydata = '') {
        $entryno = 1;
      
        $return = '';
        while($entryno <= $entrynomax) {
          $return .= $entrydata[$entryno]; # concatenate strings
          $entryno++;
        }
      
        return $return;
      }
      
    2. 将结果存储在一个数组中并使用implode返回一个包含所有项目的字符串:

      function output_month_data($entrynomax = '', $month = '', $entrydata = '') {
        $entryno = 1;
      
        $return = array();
        while($entryno <= $entrynomax) {
          $return[] = $entrydata[$entryno]; # append to array
          $entryno++;
        }
      
        return implode('', $return); # change '' to any glue you want
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多