Question

I am using PL/pgsql RETURNS TABLE to get the below output using pg_fetch_all in PHP

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)" } 
}

I am unable to use the above output in HTML. I tried using php explode but it didnt work I got zero array. Also, What confuses me I am getting quotes in first two variable and not in others.

Update

I used below function but I got zero array

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();
    }
}

How can i do this ?

Was it helpful?

Solution 2

This is the answer that I have come up with. Though it is not neat but it seems to be working as expected now

$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].'>';
}   }   }

But now the problem is raw data contains noise as in "(" so that is spoiling the output.

Thank you all for your efforts.

OTHER TIPS

Your inner values are arrays, not strings, you need to take that into account:

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.
     }
}

Depending on what you need, you'll return the accumulated result of all of those actuallyDoParsing() calls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top