基于字符串拆分为数组的多组json对象

时间:2014-03-04 21:37:43

标签: php arrays grouping

我有一个奇怪的问题要问,而且我很难绕过它。但基本上我有这个json字符串:

{
  "001-file":{
    "filename":"001 - file",
    "slugify":"001-file",
    "size":{
      "mb":60.4,
      "text":"60.4 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder3/"
  },
  "002-file":{
    "filename":"002 - file",
    "slugify":"002-file",
    "size":{
      "mb":60.2,
      "text":"60.2 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder3/"
  },
  "003-file":{
    "filename":"003 - file",
    "slugify":"003-file",
    "size":{
      "mb":60.2,
      "text":"60.2 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder3/"
  },
  "004-file":{
    "filename":"004 - file",
    "slugify":"004-file",
    "size":{
      "mb":60.2,
      "text":"60.2 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder3/"
  },
  "005-file":{
    "filename":"005 - file",
    "slugify":"005-file",
    "size":{
      "mb":60.3,
      "text":"60.3 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder4/"
  },
  "006-file":{
    "filename":"006 - file",
    "slugify":"006-file",
    "size":{
      "mb":60.2,
      "text":"60.2 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder4/"
  },
  "007-file":{
    "filename":"007 - file",
    "slugify":"007-file",
    "size":{
      "mb":60.2,
      "text":"60.2 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder4/"
  },
  "008-file":{
    "filename":"008 - file",
    "slugify":"008-file",
    "size":{
      "mb":60.3,
      "text":"60.3 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder4/"
  },
  "009-file":{
    "filename":"009 - file",
    "slugify":"009-file",
    "size":{
      "mb":60.1,
      "text":"60.1 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder4/"
  },
  "010-file":{
    "filename":"010 - file",
    "slugify":"010-file",
    "size":{
      "mb":60.1,
      "text":"60.1 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder4/"
  },
  "011-file":{
    "filename":"011 - file",
    "slugify":"011-file",
    "size":{
      "mb":60.1,
      "text":"60.1 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder5/"
  },
  "012-file":{
    "filename":"012 - file",
    "slugify":"012-file",
    "size":{
      "mb":60.2,
      "text":"60.2 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder5/"
  },
  "013-file":{
    "filename":"013 - file",
    "slugify":"013-file",
    "size":{
      "mb":60,
      "text":"60.0 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder5/"
  },
  "014-file":{
    "filename":"014 - file",
    "slugify":"014-file",
    "size":{
      "mb":59.7,
      "text":"59.7 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder5/"
  },
  "015-file":{
    "filename":"015 - file",
    "slugify":"015-file",
    "size":{
      "mb":59.9,
      "text":"59.9 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder5/"
  },
  "016-file":{
    "filename":"016 - file",
    "slugify":"016-file",
    "size":{
      "mb":56.5,
      "text":"56.5 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder5/"
  },
  "017-file":{
    "filename":"017 - file",
    "slugify":"017-file",
    "size":{
      "mb":56.3,
      "text":"56.3 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder5/"
  },
  "018-file":{
    "filename":"018 - file",
    "slugify":"018-file",
    "size":{
      "mb":56.4,
      "text":"56.4 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder6/"
  },
  "019-file":{
    "filename":"019 - file",
    "slugify":"019-file",
    "size":{
      "mb":56.5,
      "text":"56.5 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder6/"
  },
  "020-file":{
    "filename":"020 - file",
    "slugify":"020-file",
    "size":{
      "mb":56.5,
      "text":"56.5 MB"
    },
    "directory":"file:///E:/Folder1/Folder2/Folder6/"
  }
}

我需要根据目录对其进行分组。将所有E:分组,然后将所有Folder1分组,然后Folder2,然后Folder3 or Folder4 or Folder5

以下是我希望得到的一个例子:

{
  "E:":{
    "Folder1":{
      "Folder2":{
        "Folder3":{
          "001-file":{
            "filename":"001 - file",
            "slugify":"001-file",
            "size":{
              "mb":60.4,
              "text":"60.4 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder3/"
          },
          "002-file":{
            "filename":"002 - file",
            "slugify":"002-file",
            "size":{
              "mb":60.2,
              "text":"60.2 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder3/"
          },
          "003-file":{
            "filename":"003 - file",
            "slugify":"003-file",
            "size":{
              "mb":60.2,
              "text":"60.2 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder3/"
          },
          "004-file":{
            "filename":"004 - file",
            "slugify":"004-file",
            "size":{
              "mb":60.2,
              "text":"60.2 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder3/"
          }
        },
        "Folder4":{
          "005-file":{
            "filename":"005 - file",
            "slugify":"005-file",
            "size":{
              "mb":60.3,
              "text":"60.3 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder4/"
          },
          "006-file":{
            "filename":"006 - file",
            "slugify":"006-file",
            "size":{
              "mb":60.2,
              "text":"60.2 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder4/"
          },
          "007-file":{
            "filename":"007 - file",
            "slugify":"007-file",
            "size":{
              "mb":60.2,
              "text":"60.2 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder4/"
          },
          "008-file":{
            "filename":"008 - file",
            "slugify":"008-file",
            "size":{
              "mb":60.3,
              "text":"60.3 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder4/"
          },
          "009-file":{
            "filename":"009 - file",
            "slugify":"009-file",
            "size":{
              "mb":60.1,
              "text":"60.1 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder4/"
          },
          "010-file":{
            "filename":"010 - file",
            "slugify":"010-file",
            "size":{
              "mb":60.1,
              "text":"60.1 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder4/"
          }
        },
        "Folder5":{
          "011-file":{
            "filename":"011 - file",
            "slugify":"011-file",
            "size":{
              "mb":60.1,
              "text":"60.1 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder5/"
          },
          "012-file":{
            "filename":"012 - file",
            "slugify":"012-file",
            "size":{
              "mb":60.2,
              "text":"60.2 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder5/"
          },
          "013-file":{
            "filename":"013 - file",
            "slugify":"013-file",
            "size":{
              "mb":60,
              "text":"60.0 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder5/"
          },
          "014-file":{
            "filename":"014 - file",
            "slugify":"014-file",
            "size":{
              "mb":59.7,
              "text":"59.7 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder5/"
          },
          "015-file":{
            "filename":"015 - file",
            "slugify":"015-file",
            "size":{
              "mb":59.9,
              "text":"59.9 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder5/"
          },
          "016-file":{
            "filename":"016 - file",
            "slugify":"016-file",
            "size":{
              "mb":56.5,
              "text":"56.5 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder5/"
          },
          "017-file":{
            "filename":"017 - file",
            "slugify":"017-file",
            "size":{
              "mb":56.3,
              "text":"56.3 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder5/"
          }
        },
        "Folder6":{
          "018-file":{
            "filename":"018 - file",
            "slugify":"018-file",
            "size":{
              "mb":56.4,
              "text":"56.4 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder6/"
          },
          "019-file":{
            "filename":"019 - file",
            "slugify":"019-file",
            "size":{
              "mb":56.5,
              "text":"56.5 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder6/"
          },
          "020-file":{
            "filename":"020 - file",
            "slugify":"020-file",
            "size":{
              "mb":56.5,
              "text":"56.5 MB"
            },
            "directory":"file:///E:/Folder1/Folder2/Folder6/"
          }
        }
      }
    }
  }
}

有人能指出我需要去的方向才能实现这一目标吗?

这只是一个例子,真实场景可以有不同的文件夹结构和大约1000个对象。

由于

1 个答案:

答案 0 :(得分:1)

我让你整理出来。您可能会找到重构它的方法,以使其更有效并满足您的需求,但这可以满足您的需求。注释解释了每个代码块的用途。

<?php

// new array
$new_array = array();


// orginal json string
$orig_json = '{"001-file":{"filename":"001 - file","slugify":"001-file","size":{"mb":60.4,"text":"60.4 MB"},"directory":"file:///E:/Folder1/Folder2/Folder3/"},"002-file":{"filename":"002 - file","slugify":"002-file","size":{"mb":60.2,"text":"60.2 MB"},"directory":"file:///E:/Folder1/Folder4/Folder5/"}}';

echo $orig_json.'<br/>';


// convert to array
$orig_array = json_decode($orig_json, true);
//view json as array
echo '<pre>';
print_r($orig_array);
echo '</pre>';


// loop through array
foreach( $orig_array as $file_key => $file ) {
   echo ('==========================================================================');
   // view current file array
   echo '<pre>';
   print_r($file);
   echo '</pre>';

   // convert directory string into array
   $directory_structure = explode('/',$file['directory']);
   echo '<pre>';
   print_r($directory_structure);
   echo '</pre>';

   // remove uncessary array items (file:, null entries)
   foreach( $directory_structure as $key => $d ) {
      if( $d == NULL || $d == 'file:') {
          unset($directory_structure[$key]);
      }
   }

   // view the pared down array
   echo '<pre>';
   print_r($directory_structure);
   echo '</pre>';

   // build directory based array structure
   $temp_array =& $new_array;
   foreach ($directory_structure as $d) {
     // check if key already exists
     if( !array_key_exists($d, $temp_array) ) {
        $temp_array[$d] = array();
     }
     // check if at end of directory structure (last "folder")
     if( end($directory_structure) == $d ) {
         $temp_array[$d][$file_key] = $file;
     }

     // append array
     $temp_array =& $temp_array[$d];
 } 

  echo "============================================================";
}

// view new directory array
echo '<pre>';
print_r($new_array);
echo '</pre>';

// json encode
$new_json = json_encode($new_array);

// view new json string
echo $new_json.'<br/>';
?>