how to structure php nodes

Asked

Viewed 53 times

-2

I needed the nodes to stay in this format:

"name": "Baixo Alentejo",
"color": "#f0e68d\r\n"




$nodes[]='name:'.$row['name'] ."  ".'color:'.$row['color'] ;

With this code, you’re giving me:

"name:Baixo Alentejo   color:#f0e68d\r\n",
  • I’m not the one who denied your question, but I don’t understand what you’re asking.

2 answers

1

Thus?

$nodes[] =  '"name": "' . $row['name'] . '", "color": "' . $row['color'] . '"'; 
//Array
//(
//    [0] => "name": "Baixo Alentejo", "color": "#f0e68d\r\n"
//)

To format as JSON, use the json_encode:

$nodes['nodes']['name'] = $row['name'];
$nodes['nodes']['color'] = $row['color'];

$nodeEncode = json_encode($nodes); 
$nodeDecode = json_decode($nodeEncode);

echo "json_encode \n";
var_dump($nodeEncode);

echo "json_decode \n";
foreach ($nodeDecode as $node){
    echo $node->name . "\n";
    echo $node->color . "\n";
}

// json_encode 
// string(53) "{"nodes":{"name":"Baixo Alentejo","color":"#f0e68d"}}"

// json_decode 
// Baixo Alentejo
// #f0e68d

See demonstração

  • if you put it like this it looks like this ""name": "Alentejo Central", "color": "#bf664b r n"",

  • I want how json the format

  • is thus "nodes": [""name": "Alentejo Central", "color": "#bf664b r n"",""name": "Alentejo Central ", "color": "#bf664b r n"", but thus does not work. I want "nodes": [{"name":"Alentejo Central","color":"#bf664b r n"},{"name":"Alentejo Central ","color":"#bf664b r n"},

  • echo "{"; echo '"links": ', json_encode($links, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), "; echo ',"nodes": ',json_encode($Decode, JSON_PRINT | JSON_UNESCAPED_UNICODE), "; echo "}"; I do with , but I can’t get all the fields

  • @user2964140 You can create an example on http://Ideone.com and put the link here?

  • http://ideone.com/EhK8TP

  • @user2964140 See: http://ideone.com/Az8Ry5 Is that not how you want to do it? http://ideone.com/q4imCx

  • what belongs to have "nodes": [ { "name": "Alentejo Central", "color": "#bf664b r n" }, { "name": "Alentejo Central ", "color": "#bf664b r n" },

Show 3 more comments

1

If you want to generate JSON, you "must" format it as array, using the indices.

You can use various forms for this:

Form 1:

$array = ['nodes' => ["name" => "Baixo Alentejo",  "color": "#f0e68d"]];

echo $json = json_encode($array);

Form 2:

$array['nodes']["name"] = "Baixo Alentejo";
$array['nodes']["color"] = "#f0e68d";

echo $json = json_encode($array);

Form 3:

$array['nodes'][] = ["name" => "Baixo Alentejo", "color" => "#f0e68d"];

echo $json = json_encode($array);

Result (in all cases):

{"nodes":{"name":"Baixo Alentejo","color":"#f0e68d"}}

The easiest way to adapt your code would be to change the : for => and remove this as string, resulting in:

$nodes['nodes'][] = ['name' => $row['name'], 'color' => $row['color']];

echo json_encode($nodes);
  • a note for anyone using PHP before 5.4 needs to use array( => ) in place of [ => ]

  • but I am doing a database query, so the color field does not appear

  • "nodes": [ { "name": "Alentejo Central", "color": "#bf664b r n" }, { "name": "Alentejo Central ", "color": "#bf664b r n" }, outup so only Row da

  • ran this $nodes['nodes'][] = ['name' => $Row['name'], 'color' => $Row['color']]; thanks for the tip :)

Browser other questions tagged

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