A specific jQuery plugin used to render a Tree-View expects a data-string of "Nested Array of Objects". My Tree-View data (in the same structure) is available in a PHP array. I need to echo the PHP array in such way that the jQuery plugin can read the data.
I already tried to json_encode the PHP array but I get a completely different result than what's expected by the jQuery plugin. The exepected/required format of the data can be viewed here: https://mbraak.github.io/jqTree/#options-data as well as below:
var data = [
{
name: 'node1',
children: [
{ name: 'child1' },
{ name: 'child2' }
]
},
{
name: 'node2',
children: [
{ name: 'child3' }
]
}
];
My PHP array that I need to convert into the above JavaScript format (however, this is just an example):
Array
(
[1] => Array
(
[name] => CEO
[id] => 1
[children] => Array
(
[3] => Array
(
[name] => Director 1
[id] => 3
[children] => Array
(
[4] => Array
(
[name] => Senior Manager 1
[id] => 4
[children] => Array
(
[5] => Array
(
[name] => Manager 1
[id] => 5
[children] => Array
(
)
)
)
)
)
)
[6] => Array
(
[name] => Director 2
[id] => 6
[children] => Array
(
[7] => Array
(
[name] => Senior Manager 2
[id] => 7
[children] => Array
(
)
)
)
)
)
)
)
EDIT:
This is how I generate my array:
$objectTempRoles = $this->roleRepository->findAll();
$aTempRoles = [];
foreach($objectTempRoles as $oRole){
if($oRole->getIsroot() == 1){
$aTempRoles[$oRole->getUid()] = [];
$aTempRoles[$oRole->getUid()]['name'] = $oRole->getTitle();
$aTempRoles[$oRole->getUid()]['id'] = $oRole->getUid();
$aTempRoles[$oRole->getUid()]['children'] = $this->functionGetChildren($oRole);
}
}
public function functionGetChildren($oRole){
$aChildrenToReturn = [];
if($oRole->getChildren() != null && $oRole->getChildren() != false){
foreach($oRole->getChildren() as $oChild){
$aChildrenToReturn[$oChild->getUid()] = [];
$aChildrenToReturn[$oChild->getUid()]['name'] = $oChild->getTitle();
$aChildrenToReturn[$oChild->getUid()]['id'] = $oChild->getUid();
$aChildrenToReturn[$oChild->getUid()]['children'] = $this->functionGetChildren($oChild);
}
}
return $aChildrenToReturn;
}
=====
EDIT:
This is the var_dump of my array:
array (
0 =>
array (
'name' => 'CEO',
'id' => 1,
'children' =>
array (
3 =>
array (
'name' => 'Director 1',
'id' => 3,
'children' =>
array (
4 =>
array (
'name' => 'Senior Manager 1',
'id' => 4,
'children' =>
array (
5 =>
array (
'name' => 'Manager 1',
'id' => 5,
'children' =>
array (
),
),
),
),
),
),
6 =>
array (
'name' => 'Director 2',
'id' => 6,
'children' =>
array (
7 =>
array (
'name' => 'Senior Manager 2',
'id' => 7,
'children' =>
array (
),
),
),
),
),
),
)
=====
EDIT:
json_encode paramaters I've used to far: JSON_FORCE_OBJECT
=====
EDIT:
I have now succeeded in generating the required data structure. For this, I use the following function:
public function getRoleChildrenJson($aParentObject){
$json = "";
$i = 1;
foreach($aParentObject['children'] as $aObject){
$tmbObjectStr = "{name: \"".$aObject['name']."\",id: ".$aObject['id'];
if(!empty($aObject['children'])){
$tmbObjectStr .= ",children: [";
$tmbObjectStr .= $this->getRoleChildrenJson($aObject);
if($i < count($aParentObject['children'])){
$tmbObjectStr .= "]},";
}
}
else{
$tmbObjectStr .= "}]}";
}
$json .= $tmbObjectStr;
$i++;
}
return $json;
}
However, now the strangest thing happens. While the json string is now accurate, the jQUery Plugin still doesn't accept it when I first load the string through AJAX. I.e., the following does work:
var data = [
{
name: 'node1', id: 1,
children: [
{ name: 'child1', id: 2 },
{ name: 'child2', id: 3 }
]
},
{
name: 'node2', id: 4,
children: [
{ name: 'child3', id: 5 }
]
}
];
$('#rolestree').tree({
data: data
});
But, the following doesn't work:
ajaxRequest = $.ajax({
url: "/index.php" + $getData,
type: "POST",
data: "",
success: function (jsondata, textStatus, jqXHR) {
$('#rolestree').tree({
data: jsondata
});
}
});
Although the very same json string gets perfectly loaded over AJAX (I checked with console). Do I first need to eval or parse this data loaded over AJAX?
json_encode
parameters you used.json_encode()
to use it in javascript. Andarray_values()
will help you convert php arrays to "real" / zero-based numeric arrays.var_export()
, just for future reference