Answer the question
In order to leave comments, you need to log in
How to refer to the parent in JSON?
// читаем файл с закладками в формате JSON
$str = file_get_contents('Bookmarks');
$ar = json_decode($str);
$b_bar = $ar->roots->bookmark_bar->children;
$other = $ar->roots->other->children;
$synced = $ar->roots->synced->children;
$marks = array();
function recursiveJSON2ARRAY($obj){
global $marks;
foreach($obj as $v){
if(isset($v->children) && is_array($v->children) && count($v->children) > 0){
$parentname = $v->name;
foreach($v->children as $child){
if(isset($child->url)){
$marks[$child->date_added] = array('parentname'=>$parentname, 'name'=>$child->name, 'url'=>$child->url);
}
else{
recursiveJSON2ARRAY($v->children);
}
}
}
else{
if(isset($v->url)){
// здесь надо получить доступ к родителю $v (КАК???)
//$parentname = $obj->name;
$marks[$v->date_added] = array('parentname'=>$parentname, 'name'=>$v->name, 'url'=>$v->url);
}
}
}
}
if($b_bar){
recursiveJSON2ARRAY($b_bar);
}
if($other){
recursiveJSON2ARRAY($other);
}
if($synced){
recursiveJSON2ARRAY($synced);
}
//krsort($marks); // Reverse sort the bookmarks by date added
$table = '<table cellspacing="2" cellpadding="2" border="1" width="600"><tr><td><b>Date</b></td><td><b>Name</b></td><td><b>URL</b></td></tr>';
foreach($marks as $date=>$mark)
{
$table .= '<tr><td>'.date('d-m-Y', $date).'</td><td>'.$mark['parentname'].'</td><td>'.$mark['name'].'</td><td><a href="'.$mark['url'].'" target="_blank">'.$mark['url'].'</a></td></tr>';
}
$table .= '</table>';
//echo $table;
echo '<pre>';
print_r($marks);
echo '</pre>';
Answer the question
In order to leave comments, you need to log in
Here is a working example of what was needed))) Finally, I finished it.
// читаем файл с закладками в формате JSON
$str = file_get_contents('Bookmarks');
$ar = json_decode($str);
$root = $ar->roots;
$marks = array();
function normalizeChromiumTime($time) {
return date("H:i:s d-m-Y", ($time - 11644473600000000) / 1000000);
}
function recursiveJSON2ARRAY($obj, $parent = null){
global $marks;
foreach($obj as $v){
if(isset($v->children) && is_array($v->children) && count($v->children) > 0){
$parentname = $v->name;
foreach($v->children as $child){
if(isset($child->url)){
$date_added = normalizeChromiumTime($child->date_added);
$marks[$child->date_added] = array('parentname'=>$parent." -> ".$parentname, 'date'=>$date_added, 'name'=>$child->name, 'url'=>$child->url);
}
else{
recursiveJSON2ARRAY($v->children, $parent." -> ".$parentname);
}
}
}
else{
if(isset($v->url)){
$date_added = normalizeChromiumTime($v->date_added);
$marks[$v->date_added] = array('parentname'=>$parent, 'date'=>$date_added, 'name'=>$v->name, 'url'=>$v->url);
}
}
}
}
foreach($root as $childrenElement){
if(isset($childrenElement->children) && is_array($childrenElement->children)){
recursiveJSON2ARRAY($childrenElement->children, $childrenElement->name);
}
}
//krsort($marks); // Reverse sort the bookmarks by date added
$table = '<table cellspacing="2" cellpadding="2" border="1" width="600"><tr><td><b>Дата создания</b></td><td><b>Категория</b></td><td><b>Имя</b></td><td><b>URL</b></td></tr>';
foreach($marks as $date=>$mark){
$table .= '<tr><td>'.$mark['date'].'</td><td>'.$mark['parentname'].'</td><td>'.$mark['name'].'</td><td><a href="'.$mark['url'].'" target="_blank">'.$mark['url'].'</a></td></tr>';
}
$table .= '</table>';
echo $table;
Just pass the name of the parent to the function with a default parameter, whatever. And give up globals, this is harmful, nightmares begin to dream at night, early graying.
function recursiveJSON2ARRAY($obj, $parentname = null)
{
global $marks;
foreach ($obj as $v) {
if (isset($v->children) && is_array($v->children) && count($v->children) > 0) {
$parentname = $v->name;
foreach($v->children as $child) {
if(isset($child->url)){
$marks[$child->date_added] = array('parentname'=>$parentname, 'name'=>$child->name, 'url'=>$child->url);
} else {
recursiveJSON2ARRAY($v->children, $parentname);
}
}
} else {
if (isset($v->url)) {
// если $parentname не передали, то он null
$marks[$v->date_added] = array('parentname'=>$parentname, 'name'=>$v->name, 'url'=>$v->url);
}
}
}
}
if ($b_bar) {
recursiveJSON2ARRAY($b_bar);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question