【问题标题】:How to print the array value?如何打印数组值?
【发布时间】:2016-09-21 10:39:53
【问题描述】:

我有一个来自数据库的值:

 $rows = array();
  foreach($results as $result)
  {
    $rows[] = array(
        $result->fe_id,
        $result->empcode,
        $result->empname,
        $result->empphoto,
      );
      }

对于另一个功能

function theme_form_example_function($variables)
{
$output ="<h2><pre>".print_r($variables)."</pre></h2>";
return $output;

}

它给了我这样的输出

Array (
  [rows] =>
  Array ( 
    [0] => 
    Array ( 
      [0] => 1 
      [1] => 12 
      [2] => sim
      [3] => dawn-landscape-mountains-nature-large.jpg
    ) 
    [1] => 
    Array ( 
      [0] => 2 
      [1] => EMP13 
      [2] => simnav 
      [3] => download-nature-wallpaper-23.jpg
    ) 
  ) 
  [theme_hook_original] => form_example_function
)

我想将输出打印为:

Empcode: 12 EmpName: Sim Emphot: dawn-landscape-mountains-nature-large.jpg 

在第二行:

Empcode: EMP13 EmpName: Simnav Emphot: download-nature-wallpaper-23.jpg

如何使用上面的数组来实现这个输出?

【问题讨论】:

    标签: php arrays drupal-7


    【解决方案1】:

    你可以这样做:

    function theme_form_example_function($variables)
    {
        $output = '';
    
        foreach($variables['rows'] as $row) {
    
            $output .= "Empcode: " . (isset($row[1]) ? $row[1] : "--") . " ";
            $output .= "EmpName: " . (isset($row[2]) ? $row[2] : "--") . " ";
            $output .= "Emphot: " . (isset($row[3]) ? $row[3] : "--") . "<br>";     
        }
    
        return $output;
    }
    

    【讨论】:

    • 注意:未初始化的字符串偏移量:theme_form_example_function() 中的 1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2012-04-01
    相关资源
    最近更新 更多