Jump to content

utf8_decode() expects parameter 1 to be string, array given


Recommended Posts

Hi

 

I'm having an issue with this code because i can't get it to work

 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $reservas_array = array();
        $equip = array();
        $ixx = 0;
        foreach ($result as $row) {
            $id_requisicoes = $row['id_requisicoes'];
            if (!isset($reservas_array[$id_requisicoes])) {
                $reservas_array[$id_requisicoes]['id_requisicoes'] = $row['id_requisicoes'];
                $reservas_array[$id_requisicoes]['sala'] = $row['sala'];
                $reservas_array[$id_requisicoes]['data'] = $row['datas'];
                $reservas_array[$id_requisicoes]['bloco'] = $row['bloco'];
                $reservas_array[$id_requisicoes]['nome'] = $row['nome'];
                $reservas_array[$id_requisicoes]['equip'] = array();
            }
            $reservas_array[$id_requisicoes]['equip'][] = $row['equip'];
        }
        foreach($reservas_array as $id_requisicoes => $reservation)
        {
            $ixx = $ixx + 1;
            $tmp = array_map("utf8_decode", $reservation);
            $data_chegada[] = array_merge($tmp, array('num' => $ixx));

        }

 

I have the following error int that line 

$tmp = array_map("utf8_decode", $reservation);

utf8_decode() expects parameter 1 to be string, array given

 

Any help please?

Edited by gmc1103

utf8_decode() transcodes a single UTF-8 string into an ISO 8859-1 string. You cannot apply it to an array.

 

What are you trying to do, anyway? Transcoding all text from the database is a very, very odd approach, especially since ISO 8859-1 can only represent an extremely small fraction of the Unicode character set.

Hi Jacques

 

How are you?

 

Thanks for your reply

I want to print a pdf with the information of a datable

With my old code this was working, since i moved to PDO don't

This is all my code

$pdf = new Cezpdf('a4', 'landscape');
$pdf->ezSetMargins(20, 20, 20, 20);
$pdf->ezStartPageNumbers(460, 15, 10, 'right', 'Página {PAGENUM} de {TOTALPAGENUM}');
$mainFont = 'Times-Roman';
$pdf->selectFont($mainFont);
$pdf->openHere('Fit');
try {
$sql = "SELECT t1.id_requisicoes,t2.nome, t3.nome AS equip, t4.bloco, t5.sala, t6.datas
                FROM rel_requisicoes_detalhes AS t1
                INNER JOIN utilizador AS t2 ON (t1.id_utilizador = t2.id_utilizador)
                LEFT JOIN equipamento AS t3 ON (t1.id_equipamento = t3.id_equipamento)
                INNER JOIN tempos AS t4 ON (t1.id_bloco = t4.id_bloco)
                INNER JOIN salas AS t5 ON (t1.id_sala= t5.id_sala)
                INNER JOIN requisicoes AS t6 ON(t1.id_requisicoes = t6.id_requisicoes) 
                WHERE t6.`datas` = CURDATE() ORDER BY t4.`bloco`, t5.`sala`";
    $stmt = $DB_con->prepare($sql);
    if(!$stmt->execute()){
       print_r($stmt->errorInfo());
    }
    $affectedRows = $stmt->rowCount();
    if ($affectedRows <= 0) {
        $pdf->setColor(1, 0, 0);
        $sem = utf8_decode("<b>Não existem registos</b>\n");
        $pdf->addText(350, 420, 12, $sem);
    } else {
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $reservas_array = array();
        $equip = array();
        $ixx = 0;
        foreach ($result as $row) {
            $id_requisicoes = $row['id_requisicoes'];
            if (!isset($reservas_array[$id_requisicoes])) {
                $reservas_array[$id_requisicoes]['id_requisicoes'] = $row['id_requisicoes'];
                $reservas_array[$id_requisicoes]['sala'] = $row['sala'];
                $reservas_array[$id_requisicoes]['data'] = $row['datas'];
                $reservas_array[$id_requisicoes]['bloco'] = $row['bloco'];
                $reservas_array[$id_requisicoes]['nome'] = $row['nome'];
                $reservas_array[$id_requisicoes]['equip'] = array();
            }
            $reservas_array[$id_requisicoes]['equip'][] = $row['equip'];
        }
        foreach($reservas_array as $id_requisicoes => $reservation)
        {
            $ixx = $ixx + 1;
            $tmp = array_map("utf8_decode", $reservation);
            $data_chegada[] = array_merge($tmp, array('num' => $ixx));

        }
    }
} catch (PDOException $e) {
    echo $e->getMessage();
}

$titles = array(
        'sala' => '<b>Sala</b>',
	    'nome' => '<b>Docente</b>',
	    'data' => '<b>Data</b>',
	    'bloco' => utf8_decode('<b>Início</b>'),
	    'equip' =>utf8_decode('<b>Equipamento</b>')
    );

$parametros_adicionales = array(
        'width'=>570, 
        'showHeadings'=>1,
        'fontSize' => 9,
        'colGap'=>2,
        'xPos' => 'center',
        'xOrientation'=>'center', 
        'cols'=>array(
        "sala" => array('justification'=>'center', 'width' => '100'),
        "nome" => array('justification'=>'left', 'width' => '200'),
        "data" => array('justification'=>'center', 'width' => '100'), 
        "bloco" => array('justification'=>'center', 'width' => '60'),
        "equip" => array('justification'=>'left', 'width' => '110')));

$escola = utf8_decode("<b>N.º do Código do Estabelecimento de Ensino xxxx/ 201</b>\n");
$contatos = utf8_decode("<b>N.º de Telefone:xxxxxx / Fax:xxxxx</b>\n");


//Posição
$pdf->ezImage("../../../img/logo_pdf.jpg", 0,50, 'none', 'left');
$pdf->selectFont("fonts/Helvetica-Bold.afm");
$pdf->addText(250, 480, 11, $escola);
$pdf->addText(280, 460, 11, $contatos);
$pdf->setLineStyle(1);
$pdf ->line(80, 450, 750, 450);
$pdf->ezSetY(440);
$todayDate = date("d-m-Y",strtotime("tomorrow"));
$textoReq= ("Requisições do dia " .$todayDate);
$pdf->addText(80, 430, 11, $textoReq);
$pdf->ezSetY(415);
$pdf->ezTable($data_chegada, $titles, '', $parametros_adicionales);
$pdf->addInfo('Author', 'GMC');
ob_end_clean();
$impresso = "<b>Impresso em:</b> ".date("d/m/Y - H:i");
$pdf->addText(340, 30, 11, $impresso);
if (isset($_GET['d']) && $_GET['d']){
	$pdfcode = $pdf->ezOutput(1);
	$pdfcode = str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
	echo '<html><body>';
	echo trim($pdfcode);
	echo '</body></html>';
} else {
	$pdf->ezStream();
}
?>

The problem is in that line because using var_dump in $reservation is ok, var_dump in $tmp no, check the results

 

var_dump($reservation)

array (size=6)
'id_requisicoes' => string '4' (length=1)
'sala' => string '1' (length=1)
'data' => string '2016-09-15' (length=10)
'bloco' => string '08:15 - 09:45' (length=13)
'nome' => string 'XXXXX' (length=12)
'equip' =>
array (size=2)
0 => string 'Projetor' (length=8)
1 => string 'Leitor / CD/ USB' (length=16)

 

var_dump($tmp)
array (size=6)
'id_requisicoes' => string '4' (length=1)
'sala' => string '1' (length=1)
'data' => string '2016-09-15' (length=10)
'bloco' => string '08:15 - 09:45' (length=13)
'nome' => string 'XXXXXXX' (length=12)
'equip' => null

 

Check the "equip" in both

As I already said, you cannot apply utf8_decode() to an array (like the one behind the equip key). It only works with a single string. If you want a recursive version of array_map() or utf8_decode(), your need to implement it yourself.

 

And again: The whole approach is suspect. When you have Unicode text in your database, the PDF should also use Unicode. Have you checked if there's a configuration setting for the character encoding? If there isn't one, have you tried to modify the PDF library? If that's too difficult, have you looked for a better library?

 

A five-second Google search has turned up this workaround.

Hi Jacques1

 

The error is there for sure in the "array_map" but my main concern is this

var_dump($reservation)
array (size=6)
'id_requisicoes' => string '4' (length=1)
'sala' => string '1' (length=1)
'data' => string '2016-09-15' (length=10)
'bloco' => string '08:15 - 09:45' (length=13)
'nome' => string 'XXXXX' (length=12)
'equip' =>
array (size=2)
0 => string 'Projetor' (length=
1 => string 'Leitor / CD/ USB' (length=16)
 
var_dump($tmp)
array (size=6)
'id_requisicoes' => string '4' (length=1)
'sala' => string '1' (length=1)
'data' => string '2016-09-15' (length=10)
'bloco' => string '08:15 - 09:45' (length=13)
'nome' => string 'XXXXXXX' (length=12)
'equip' => null

So, why it loose the last element??

Because that's exactly what the PHP error is all about, for heaven's sake!

 

Do you think PHP displays error messages just for fun? When you get an error message, that means there's really, actually something wrong. You're trying to apply utf8_encode to an array (the one behind the equip key). This is invalid, so you get an error and a nonsense result, namely null. The equip array after applying utf8_decode is null.

<?php

$before = ['a', 'b'];
var_dump($before);

$after = utf8_decode($before);
var_dump($after);

Got it?

 

But none of this matters, because the approach as a whole is wrong (as I've been trying to tell you since the beginning of this thread).

Edited by Jacques1

Hi Jacques

 

You are absolutly right, i was tryig to modify only for PDO but not every code works with some changes.

For what is the best approach to get what i want into a pdf? My DB has utf-8 character set and utf bin as collation,the engine is InnoDB 

 

Thanks again

When you have Unicode text in your database, the PDF should also use Unicode. Have you checked if there's a configuration setting for the character encoding? If there isn't one, have you tried to modify the PDF library? If that's too difficult, have you looked for a better library?

 

A five-second Google search has turned up this workaround.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.