I need to build an array-within-objects-within-array-within-objects for JavaScript uses. My data is in PHP, and I could organize my necessary information using a foreach loop in an array:
$officeWiseEmployees = array();
foreach($employeeList as $employee) :
$officeWiseEmployees[$employee->office_name][$employee->employee_id] = $employee->employee_name;
endforeach;
My intention was to make arrays, then I'll typecast with (object)
where necessary to make the objects within. By the way the resulted data is:
array:2 [
"Office 1" => array:2 [
1 => "User 1"
2 => "User 2"
]
"Office 2" => array:2 [
3 => "User 3"
4 => "User 4"
]
]
The desired array-within-objects...:
[{
text: 'Office Name 1',
children:
[ { value: 1, text: 'Employee 1' }, { value: 2, text: 'Employee 2' } ]
},
{
text: 'Office Name 2',
children:
[ { value: 3, text: 'Employee 3' }, { value: 4, text: 'Employee 4' } ]
}]
Issue is, whenever I wanted to proceed to assign the Array Indexes, I's failing within the foreach loops.
$make_up_array = array();
foreach( $officeWiseEmployees as $office_name => $office_employees ) {
$make_up_array['text'] = $office_name;
foreach( $office_employees as $employee_id => $employee_name ) {
// dump($employee_id);
// dump($employee_name);
}
}
You can see, on line 3 I's doing wrong, so I's getting only the last Office name under text
index. If I introduce another index over text
or children
, then I's getting further beyond the setup I needed.
How can I convert my PHP array into a JavaScript array-within-objects-within-array-within-objects in an easy way? I thought it could be easier than I might think of, that's why developer used this.
As others have mentioned, json_encode
will work out whether something is an array or object.
A lot of the complexity comes in the way you set the user id to the key of each element.
<?php
$employees = [
['id' => 1, 'name' => 'Employee 1', 'office' => 'Office 1'],
['id' => 2, 'name' => 'Employee 2', 'office' => 'Office 1'],
['id' => 3, 'name' => 'Employee 3', 'office' => 'Office 2'],
['id' => 4, 'name' => 'Employee 4', 'office' => 'Office 1'],
['id' => 5, 'name' => 'Employee 5', 'office' => 'Office 2'],
];
$offices = [];
foreach ($employees as $employee) {
$offices[$employee['office']][] = [$employee['id'] => $employee['name']];
}
$officesOutput = [];
foreach ($offices as $name => $officeEmployees) {
$employees = [];
foreach ($officeEmployees as $employeeName) {
$employees[] = [
'value' => key($employeeName),
'text' => current($employeeName)
];
}
$officesOutput[] = [
'text' => $name,
'children' => $employees
];
}
echo json_encode($offices, JSON_PRETTY_PRINT);
echo PHP_EOL;
echo json_encode($officesOutput, JSON_PRETTY_PRINT);
Results in:
{
"Office 1": [
{
"1": "Employee 1"
},
{
"2": "Employee 2"
},
{
"4": "Employee 4"
}
],
"Office 2": [
{
"3": "Employee 3"
},
{
"5": "Employee 5"
}
]
}
[
{
"text": "Office 1",
"children": [
{
"value": 1,
"text": "Employee 1"
},
{
"value": 2,
"text": "Employee 2"
},
{
"value": 4,
"text": "Employee 4"
}
]
},
{
"text": "Office 2",
"children": [
{
"value": 3,
"text": "Employee 3"
},
{
"value": 5,
"text": "Employee 5"
}
]
}
]
You don't have to do all this complex stuff tbh. Just use your original data (after the first foreach) and then
$sendtoclient = json_encode($mydeeparray) ;
And then in JS
myobj = JSON.parse(<? echo $mydeeparray? ;>
Then you can do whatever you want with the object.
If you struggling constructing the data into the right form, just do it clientside if its not crucial. Its very popular to do things like sorting, ordering clientside.
And, for your problem: You have to create at least an object in the array or you will just override $array['text']. I am not familiar with object oriented PHP, but you have to do something like this (pseudocode)
§arr = array();
foreach($officewiseemployees as $office_name => $employees){
$obj = Object();
$obj->name =$office_name;
$obj->employees = Array();
foreach($employees as $employeeId => $employeename)
$employeeObject = new Object();
//Assign your elements to the Object
$obj->employees.append($employeeObject);
}
}
This is just pseudocode, but it should look like this.
Try creating the objects as objects, then adding them into the arrays in the right place. Then PHP's json_encode()
function should do what you expect.
This line: $make_up_array['text'] = $office_name;
If the idea is to add a new $office_name each time around the loop then try it like this instead:
$make_up_array['text'][] = $office_name;
In general, try creating just the top level elements at first and check the JSON output looks correct, then add in extra elements one at a time - you are probably pretty close to what you need!