Vinfoster0701 Vinfoster0701 - 2 months ago 11
JSON Question

Push data into a javascript array from json_encode PHP

I am using a library that requires this kind of structure

var items = [
new primitives.orgdiagram.ItemConfig({
id: 0,
parent: null,
title: "Scott Aasrud",
description: "Root",
phone: "1 (416) 001-4567",
email: "scott.aasrud@mail.com",
image: "demo/images/photos/a.png",
itemTitleColor: primitives.common.Colors.RoyalBlue
}),
new primitives.orgdiagram.ItemConfig({
id: 1,
parent: 0,
title: "Ted Lucas",
description: "Left",
phone: "1 (416) 002-4567",
email: "ted.lucas@mail.com",
image: "demo/images/photos/b.png",
itemTitleColor: primitives.common.Colors.RoyalBlue
}),
];

options.items = items;


How can I push data retrieved from my PHP json to the array? The one that confuses me is the

new primitives.orgdiagram.ItemConfig


The json that is retrieved from my PHP is this part

id: 0,
parent: null,
title: "Scott Aasrud",
description: "Root",
phone: "1 (416) 001-4567",
email: "scott.aasrud@mail.com",
image: "demo/images/photos/a.png",
itemTitleColor: primitives.common.Colors.RoyalBlue


can somebody help me please??

thank you :)

Answer

Try below code. In case JSON has multiple objects use a for loop to push the values into the array.

var items = [];

var item1 = new primitives.orgdiagram.ItemConfig({
  id: 0,
  parent: null,
  title: "Scott Aasrud",
  description: "Root",
  phone: "1 (416) 001-4567",
  email: "scott.aasrud@mail.com",
  image: "demo/images/photos/a.png",
  itemTitleColor: primitives.common.Colors.RoyalBlue
});

items.push(item1);
Comments