【问题标题】:How to use php array+string in html?如何在html中使用php数组+字符串?
【发布时间】:2014-03-18 20:44:11
【问题描述】:

我正在使用 PL/pgsql RETURNS TABLE 在 PHP 中使用 pg_fetch_all 获得以下输出

array(4) { 
[0]=> array(1) 
    { ["not_actual_values"]=> string(88) "("var1","var2",var3,var4,date1,int,int,int,var5,int,int,int,int)" } 
[1]=> array(1) 
    { ["not_actual_values"]=> string(89) "("var1","var2",var3,var4,date1,int,int,int,var5,int,int,int,int)" } 
[2]=> array(1) 
    { ["not_actual_values"]=> string(88) "("var1","var2",var3,var4,date1,int,int,int,var5,int,int,int,int)" } 
[3]=> array(1) 
    { ["not_actual_values"]=> string(89) "("var1","var2",var3,var4,date1,int,int,int,var5,int,int,int,int)" } 
}

我无法在 HTML 中使用上述输出。我尝试使用 php explode 但它没有工作我得到了 zero 数组。另外,让我感到困惑的是,我在前两个变量中得到引号,而不是在其他变量中。

更新

我使用了下面的函数,但我得到了 zero 数组

function pgArrayToPhp($text) {
    if(is_null($text)) {
        return array();
    } else if(is_string($text) && $text != '{}') {
        $text = substr($text, 1, -1);// Removes starting "{" and ending "}"
        if(substr($text, 0, 1) == '"') {
            $text = substr($text, 1);
        }
        if(substr($text, -1, 1) == '"') {
            $text = substr($text, 0, -1);
        }
        // If double quotes are present, we know we're working with a string.
        if(strstr($text, '"')) { // Assuming string array.
            $values = explode('","', $text);
        } else { // Assuming Integer array.
            $values = explode(',', $text);
        }
        $fixed_values = array();
        foreach($values as $value) {
            $value = str_replace('\\"', '"', $value);
            $fixed_values[] = $value;
        }
        return $fixed_values;
    } else {
        return array();
    }
}

我该怎么做?

【问题讨论】:

  • 请让您的代码更易于阅读。像 1 个命令的新行
  • 分享您的代码。不可能说你做错了什么。
  • 我需要在 html 表格中使用上面的输出。我需要在单独的列中使用 each 变量。现在我把它们放在一列中。
  • 将“值”字符串解析为 CSV 可能更可靠。 PHP 有一个你可能会发现有用的函数:php.net/manual/en/function.str-getcsv.php。这个我没试过。
  • 等一下,您正在尝试解码var_dump() 输出?为什么?

标签: php html arrays postgresql plpgsql


【解决方案1】:

您的内部值是数组,而不是字符串,您需要考虑到这一点:

function pgArraytoPhp($array) {
     foreach ($array as $row) {      // $row is an inner array here!
         actuallyDoParsing($row[0]); // Parse row's first element, 
                                     // which will be the string you want.
     }
}

根据您的需要,您将返回所有actuallyDoParsing() 调用的累积结果。

【讨论】:

    【解决方案2】:

    这是我想出的答案。虽然它不整洁但它现在似乎按预期工作

    $fetch=pg_fetch_all($query); //fetching data from postgresql
    $count=count($fetch);
    for ($j=0;$j<$count;$j++) {
    $a = $fetch[$j]['not_actual_values'];
    $b = array(explode("," , $a)); 
    $count2=count($b[0]);
    foreach ($b as $f) { echo '<tr>';
    for ($i=0;$i<$count2;$i++){
    echo '<td><input type="text" readonly value='.$f[$i].'>';
    }   }   }
    

    但现在的问题是原始数据包含 "(" 中的噪声,因此会破坏输出。

    感谢大家的努力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多