【问题标题】:PHP how to get the content of an array inside a functionPHP如何获取函数内部数组的内容
【发布时间】:2016-03-09 09:40:06
【问题描述】:

我正在尝试更多地了解数组和 foreach 语句。

在一个文件中,我有以下类和函数:

class Onboarding_Dashboard {

static function dashboard_onboarding_content() {
    $items = array(
        'stage-1' => array(
            'title' => 'Hello',
            'content' => 'This is some content text',
            'show-next' => 'true',
            'show-prev' => 'true',
        ),
        'stage-2' => array(
            'title' => 'Hello',
            'content' => 'This is some content text',
            'show-next' => 'true',
            'show-prev' => 'true',
        )


    )
}    
}

在另一个文件中,我想使用它的内容来创建一个 foreach 语句。像这样的:

static function action_get_content() {
    $items = Onboarding_Dashboard::dashboard_onboarding_content();
    foreach ($items as $item) {
        echo '<h2>'.$item['title'].'</h2>';
        echo '<p>'.$item['content'].'</p>';
    }

}

但我不确定我还需要添加什么才能从我的类函数dashboard_onboarding_content 中获取数据

更新:我更新了我的 foreach 语句,并将其包装在一个函数中。我如何在我的代码中的另一个点选择这个函数的结果 - 是否像做一样简单

echo action_get_content();

【问题讨论】:

    标签: php arrays foreach


    【解决方案1】:

    编辑以回答 OP 的评论

    当操作员询问如何创建另一个函数来处理他可以回显的 foreach 时,我添加了 dashboard_foreach_example() 函数。

    class Onboarding_Dashboard {
    
        static function dashboard_onboarding_content(){
            $items = array(
                'stage-1' => array(
                    'title' => 'Hello',
                    'content' => 'This is some content text',
                    'show-next' => 'true',
                    'show-prev' => 'true',
                ),
                'stage-2' => array(
                    'title' => 'Hello',
                    'content' => 'This is some content text',
                    'show-next' => 'true',
                    'show-prev' => 'true',
                )
            );
    
            return $items;
        }    
    
        static function dashboard_foreach_example(){
            $items = self::dashboard_onboarding_content();
            $output = '';
            foreach ($items as $item) {
                $output .= '<h2>'.TITLE ATTRIBUTE.'</h2>';
                $output .= '<p>'.TITLE ATTRIBUTE.'</p>';
            }
    
            return $output;
        }
    }
    

    您现在可以简单地回显dashboard_foreach_example(),因为它将在函数中生成输出

    echo Onboarding_Dashboard::dashboard_foreach_example();
    

    【讨论】:

    • 这与我实际到达的位置很接近。快速的问题,假设我想将我的 foreach 语句包装在一个函数 (action_get_content) 中,我如何在其他地方回显它 - 我将更新我的问题,以便您了解我的意思
    • 太棒了,这行得通 - 我是否必须将 dashboard_foreach_example 保存在与上述函数相同的文件中?
    • 因为它现在是同一个类中的一个函数,因此不能真正拆分,但是您可以创建一个新函数,只要您将$items 更改为实际到达dashboard_onboarding_content()函数
    • 所以如果我将 $items 更改为 $items = Onboarding_Dashboard::dashboard_onboarding_content();那么它应该在另一个类中工作吧?
    • 没错 :) 你明白了
    【解决方案2】:

    更改您的函数以返回数组:

    class Onboarding_Dashboard {
       static function dashboard_onboarding_content() {
          // your $items array stays here
          return $items;
       }
    }
    
    function action_get_content() {
       $html = '';
       foreach (Onboarding_Dashboard::dashboard_onboarding_content() as $item) {
          $html .= '<h2>'.$item['title'].'</h2>';
          $html .= '<p>'.$item['title'].'</p>';
       }
       return $html;
    }
    

    然后你这样输出:

    echo action_get_content();
    

    或者,您可以将此功能作为课程的一部分:

    class Onboarding_Dashboard {
       static function dashboard_onboarding_content() {
          // your $items array stays here
          return $items;
       }
    
       static function action_get_content() {
          $html = '';
          foreach (self::dashboard_onboarding_content() as $item) {
             $html .= '<h2>'.$item['title'].'</h2>';
             $html .= '<p>'.$item['title'].'</p>';
          }
          return $html;
       }
    }
    

    然后你这样输出:

    echo Onboarding_Dashboard::action_get_content();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 2019-10-23
      • 2015-11-26
      • 2015-03-20
      相关资源
      最近更新 更多