generate a php json

Asked

Viewed 102 times

0

hello

I have a problem I believe you can help me, is the following need to have a json of this genus

    {
"nodes":[
{"name":"Barry"},
{"name":"Frodo"},
{"name":"Elvis"},
{"name":"Sarah"},
{"name":"Alice"}
],
"links":[
{"source":"Barry","target":"Elvis","value":2},
{"source":"Frodo","target":"Elvis","value":2},
{"source":"Frodo","target":"Sarah","value":2},
{"source":"Barry","target":"Alice","value":2},
{"source":"Elvis","target":"Sarah","value":2},
{"source":"Elvis","target":"Alice","value":2},
{"source":"Sarah","target":"Alice","value":4}
]}

I’m generating in this way

             $stmt->execute();
        $nodes = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $stmptrabalho->execute();
            $links = $stmptrabalho->fetchAll(PDO::FETCH_ASSOC);
          $array = array(
        'nodes' => $nodes,
        'links' => $links
    );
$fp = fopen('json.json', 'w');
fwrite($fp, json_encode($array,JSON_UNESCAPED_UNICODE));
fclose($fp);

generates me the file only it is all on the same line there is some way to save as the file above another thing happens to me is the value appear with "in value and should not"

new result

 "nodes": [
    {
        "name": "Alentejo Central"
    },
    {
        "name": "Alentejo Litoral"
    },
    {
        "name": "Algarve"
    },
    {
        "name": "Alto Alentejo"
    },
    {
        "name": "Alto Trás-os-Montes"
    },
    {
        "name": "Ave"
    },
    {
        "name": "Baixo Alentejo"
    },
    {
        "name": "Baixo Mondego"
    },
    {
        "name": "Baixo Vouga"
    },
    {
        "name": "Beira Interior Norte"
    },
    {
        "name": "Beira Interior Sul"
    },
    {
        "name": "Cávado"
    },
    {
        "name": "Cova da Beira"
    },
    {
        "name": "Dão-Lafões"
    },
    {
        "name": "Douro"
    },
    {
        "name": "Entre Douro e Vouga"
    },
    {
        "name": "Estrangeiro"
    },
    {
        "name": "Grande Lisboa"
    },
    {
        "name": "Grande Porto"
    },
    {
        "name": "Lezíria do Tejo"
    },
    {
        "name": "Médio Tejo"
    },
    {
        "name": "Minho-Lima"
    },
    {
        "name": "nao se aplica"
    },
    {
        "name": "Não se aplica"
    },
    {
        "name": "Oeste"
    },
    {
        "name": "Península de Setúbal"
    },
    {
        "name": "Pinhal Interior Norte"
    },
    {
        "name": "Pinhal Interior Sul"
    },
    {
        "name": "Pinhal Litoral"
    },
    {
        "name": "Região Autónoma da Madeira"
    },
    {
        "name": "Região Autónoma dos Açores"
    },
    {
        "name": "Serra da Estrela"
    },
    {
        "name": "Tâmega"
    }
],
"links": [
    {
        "source": "Alentejo Central",
        "target": "Alentejo Central",
        "value": "3939"
    },
    {
        "source": "Alentejo Central",
        "target": "Alentejo Litoral",
        "value": "47"
    },
    {
        "source": "Alentejo Central",
        "target": "Algarve",
        "value": "113"
    },

inserir a descrição da imagem aquithis photo is what I have in the postgres concerning the data I want to show in sankety

1 answer

0

json_encode allows the use of bitmask to pass several constants to this method. I have now tested so:

<?php
$array = json_decode('{
"nodes":[
{"name":"Barry"},
{"name":"Frodo"},
{"name":"Elvis"},
{"name":"Sarah"},
{"name":"Alice"}
],
"links":[
{"source":"Barry","target":"Elvis","value":2},
{"source":"Frodo","target":"Elvis","value":2},
{"source":"Frodo","target":"Sarah","value":2},
{"source":"Barry","target":"Alice","value":2},
{"source":"Elvis","target":"Sarah","value":2},
{"source":"Elvis","target":"Alice","value":2},
{"source":"Sarah","target":"Alice","value":4}
]}');

$fp = fopen('json.json', 'w');
fwrite($fp, json_encode($array,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
fclose($fp);

Giving this result:inserir a descrição da imagem aqui

  • { "source": "T u00e2mega", "target": "Regi u00e3o Aut u00f3noma dos A u00e7ores", "value": "28" }, { "source": "T u00e2mega", "target": "Star Saw", "value": "4" }, { "source": "T u00e2mega", "target": "T u00e2mega", "value": "12920"&#Xa"; }

  • gets the middle part separated and desecrated fields

  • I tested now, I show the result the above

  • I’ll go through with it

  • what may be happening in my case will be that I have read well done php I think so

  • Exactly, like mine. If you want a more customised json output I don’t think I can help you.

  • May I ask why this need?

  • I have a paper on D3.js that is the necessity

  • I don’t know. It doesn’t work this way?

  • no. friend tried to put up less data and nothing

  • convert this query to Pdo $nodes = array(); for ($x = 0; $x < mysql_num_rows($query); $x++) { $nodes[] = mysql_fetch_assoc($query); }

  • How are you holding up the result of json.json? That’s how it is: d3.json("json.json", function(data) {... ?

  • yes it is but I saw that I can call php directly .

  • Yes you can, just do the echo json

  • yes that’s what I did and works with the example now with my data is that no and I do not know why..

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.