generate a json php

0

Hello

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

    {
"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 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);

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

this photo is what I have in postgres regarding the data I want to display on sankety

    
asked by anonymous 25.05.2016 / 12:47

1 answer

0

json_encode allows the use of bitmask to pass multiple constants to this method. I tested it this way:

<?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:

    
25.05.2016 / 12:57