Convert PHP Array to JSON Tree

Convert PHP array to JSON tree

My solution:

$data = array(
array('id' => 1, 'parent_id' => null, 'name' => 'lorem ipsum'),
array('id' => 2, 'parent_id' => 1, 'name' => 'lorem ipsum1'),
array('id' => 3, 'parent_id' => 1, 'name' => 'lorem ipsum2'),
array('id' => 4, 'parent_id' => 2, 'name' => 'lorem ipsum3'),
array('id' => 5, 'parent_id' => 3, 'name' => 'lorem ipsum4'),
array('id' => 6, 'parent_id' => null, 'name' => 'lorem ipsum5'),
);

$itemsByReference = array();

// Build array of item references:
foreach($data as $key => &$item) {
$itemsByReference[$item['id']] = &$item;
// Children array:
$itemsByReference[$item['id']]['children'] = array();
// Empty data class (so that json_encode adds "data: {}" )
$itemsByReference[$item['id']]['data'] = new StdClass();
}

// Set items as children of the relevant parent item.
foreach($data as $key => &$item)
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
$itemsByReference [$item['parent_id']]['children'][] = &$item;

// Remove items that were added to parents elsewhere:
foreach($data as $key => &$item) {
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
unset($data[$key]);
}

// Encode:
$json = json_encode($data);

PHP array to Json tree format

Try this quickie conversion function

function fixit($yourArray) {
$myArray = array();
foreach ($yourArray as $itemKey => $itemObj) {
$item = array();
foreach ($itemObj as $key => $value) {
if (strtolower($key) == 'children') {
$item[$key] = fixit($value);
} else {
$item[$key] = $value;
}
}
$myArray[] = $item;
}
return $myArray;
}

$fixed = fixit($my_array);
$json = json_encode($fixed);

Conversion form array to json for tree format. (php)

Try this:

<?php

function getTree($path) {

$dir = scandir($path);

$items = array();

foreach($dir as $v) {

// Ignore the current directory and it's parent
if($v == '.' || $v == '..')
continue;

$item = array();

// If FILE
if(!is_dir($path.'/'.$v)) {

$fileName = basename($v);
$file = array();
$file['name'] = $fileName;
$file['size'] = '122';

$item = $file;

} else {
// If FOLDER, then go inside and repeat the loop

$folder = array();
$folder['name'] = basename($v);
$childs = getTree($path.'/'.$v);
$folder['children'] = $childs;

$item = $folder;

}

$items[] = $item;

}

return $items;
}

$path = 'data';
$tree['name'] = 'Main node';
$tree['children'] = getTree($path);

$json = json_encode($tree, JSON_PRETTY_PRINT);

echo '<pre>';
echo $json;
echo '</pre>';

?>

PHP create json file tree structure from flat array

My approach would be:

 $res=[]; 
function flatten ($in, $dir, $dots, &$result) {
$tmp = [];
if (is_array($in)) {
$tmp[0] = $dir;
$tmp[1] = $dots.'Directory'; //displaytext
$tmp[2] = false;
$result[] = $tmp;
$dots = $dots.'.. ';
foreach($in as $k => $v) flatten($v, $dir.'/'.$k, $dots, $result);
} else {
$tmp[0] = $dir.'/'.$in;
$tmp[1] = $dots.$in; //displaytext
$tmp[2] = true;
$result[] = $tmp;
}
}
flatten($array, '','', $res); //$array is the array you created
echo json_encode($res);

But the code is not tested. The idea is to concationate the keys (directories) recursively, until you reach the leafs (files), and store everything in an array that gets passed by reference.

The last edit should finally fix it.

PHP walk through flat array to create json string

You need to do it like below:-

<?php

$final_array = [['/', '/', 'false']]; // i have taken first value from output by-default because i am unable to create any logic for first value through the given input

foreach($array as $key=>$val){
if($key=='0'){
$final_array[] = ['/'.$val,$val,'true'];
}else{
$exploded_key = explode('.',$key);
foreach ($exploded_key as $k=>$v){
if($v =='0'){
$dots = '.. ';
for ($i=0;$i<count(array_slice($exploded_key, 0, $k));$i++){
$dots .= '.. ';
}
$final_array[] = [ '/'. join('/', array_slice($exploded_key, 0, $k)).'/'.$val,$dots.$val,'true'];
}else{
$dots = '.. ';
for ($i=0;$i<count(array_slice($exploded_key, 0, $k));$i++){
$dots .= '.. ';
}
$final_array[] = [ '/'. join('/', array_slice($exploded_key, 0, $k+1)),$dots.$v,'false'];
}
}
}
}

echo "<pre/>";print_r($final_array);

Output:- https://eval.in/890536

Note:- if you want json as output then use json_encode() like below:-

echo json_encode($final_array);

Output:- https://eval.in/890584

Encode array to JSON string without array indexes

You want PHP's array_values() function:

$json_out = json_encode(array_values($your_array_here));

How to convert an array to object in PHP?

This one worked for me

  function array_to_obj($array, &$obj)
{
foreach ($array as $key => $value)
{
if (is_array($value))
{
$obj->$key = new stdClass();
array_to_obj($value, $obj->$key);
}
else
{
$obj->$key = $value;
}
}
return $obj;
}

function arrayToObject($array)
{
$object= new stdClass();
return array_to_obj($array,$object);
}

usage :

$myobject = arrayToObject($array);
print_r($myobject);

returns :

    [127] => stdClass Object
(
[status] => Have you ever created a really great looking website design
)

[128] => stdClass Object
(
[status] => Figure A.
Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution.
)

[129] => stdClass Object
(
[status] => The other day at work, I had some spare time
)

like usual you can loop it like:

foreach($myobject as $obj)
{
echo $obj->status;
}


Related Topics



Leave a reply



Submit