postgresql - PHP json_encode Problem with Backslash and Array Name

806

I'm converting some postgresql data to PHP json_encode, but I have some problems:

  • json_encode adds an BackSlash to all slashes that I have in my data.

  • In descriptions apears the close of paragraph tag , i think because the backslashes problem...

  • and i don't want my array inside the object named with index "0" but with the name "attach:"

MY JSON OUTPUT:

{"arns":[{"arn":"CSC-ECN-NAUB109","problem":"description problem<\/p>",
"solution":"solution description<\/p>",
   "0":[{"name":"jquery.png","path":"http:\/\/arn.test.pt\/uploads\/CSC-ECN-NAUB109\/jquery.png"}]}]} 

MY CODE:

<?php 

require('includes/connection.php');

$modelos_info = (isset($_GET['models'])) ? $_GET['models'] : "none";

if($modelos_info != "none"){

$sth = $dbh->query("SELECT * FROM arn_info JOIN upload2 ON (arn_info.arn=upload2.id_arn) WHERE modelos LIKE '$modelos_info ;%' OR modelos LIKE '%; $modelos_info ;%' ");

$sth->setFetchMode(PDO::FETCH_ASSOC);

$response = array();
$posts = array();

while($row = $sth->fetch())
{ 

$arn=$row ['arn'];
$problem=$row['problem'];
$solution=$row['solution'];
$name=$row['name'];
$path=$row['path'];

$posts_anexos['attach'] = $posts2;

$posts2[] = array('name'=> $name , 'path'=> 'http://arn.test.pt/' .$path);
$posts[] = array('arn'=> $arn , 'problem'=> $problem , 'solution'=> $solution, $posts2 );
} 

$response['arns'] = $posts;

$fp = fopen('arns.json', 'w');

fwrite($fp, json_encode($response));

fclose($fp);

echo json_encode($response);

}
?>

Thanks

People are also looking for solutions to the problem: javascript - Execute link automatically in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.