php - 使用下划线值字符串从数组创建多层 json 结构
问题描述
如果我有一个包含如下数据的数组,那么创建多层 json 的最佳方法是什么?数据基本上以数组格式出现,我试图根据下划线提供的层次结构动态创建 json。
0 => 'Business Services',
1 => 'Business Services_Consulting',
2 => 'Business Services_Foodservice Distribution',
3 => 'Business Services_Information Services',
4 => 'Consumer',
5 => 'Consumer_Consumer Discretionary',
6 => 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories',
7 => 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Activewear',
8 => 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Apparel',
9 => 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Footwear',
10 => 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Luxury Goods',
11 => 'Consumer_Consumer Discretionary_Broadline Retail',
12 => 'Consumer_Consumer Discretionary_Consumer Services',
13 => 'Consumer_Consumer Discretionary_Consumer Services_Child Care',
14 => 'Consumer_Consumer Discretionary_Consumer Services_Fitness Services/Gyms',
15 => 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure',
16 => 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Cruise Lines',
17 => 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Gaming',
18 => 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Live Entertainment',
19 => 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Lodging',
20 => 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Online Sports Betting',
21 => 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Theme Parks',
22 => 'Consumer_Consumer Discretionary_Recreational Products',
23 => 'Consumer_Consumer Discretionary_Recreational Products_Fitness Equipment',
24 => 'Consumer_Consumer Discretionary_Recreational Products_Recreational Vehicles',
25 => 'Consumer_Consumer Discretionary_Recreational Products_Sporting Goods - Recreational Products',
26 => 'Consumer_Consumer Discretionary_Recreational Products_Toys',
27 => 'Consumer_Consumer Discretionary_Restaurants',
28 => 'Consumer_Consumer Discretionary_Restaurants_Casual Dining',
29 => 'Consumer_Consumer Discretionary_Restaurants_Fast Casual',
30 => 'Consumer_Consumer Discretionary_Restaurants_Full-Service Dining',
31 => 'Consumer_Consumer Discretionary_Restaurants_Ghost Kitchen',
32 => 'Consumer_Consumer Discretionary_Restaurants_QSR',
33 => 'Consumer_Consumer Discretionary_Specialty Retail',
34 => 'Consumer_Consumer Discretionary_Specialty Retail_Auto Retail',
35 => 'Consumer_Consumer Discretionary_Specialty Retail_Auto Retail_Online Auto Marketplaces',
36 => 'Consumer_Consumer Discretionary_Specialty Retail_Consumer Electronics',
37 => 'Consumer_Consumer Discretionary_Specialty Retail_Food Retail',
38 => 'Consumer_Consumer Discretionary_Specialty Retail_Home Furnishings',
39 => 'Consumer_Consumer Discretionary_Specialty Retail_Home Improvement',
40 => 'Consumer_Consumer Discretionary_Specialty Retail_Sporting Goods - Specialty Retail',
41 => 'Consumer_Consumer Staples',
42 => 'Consumer_Consumer Staples_Household & Personal Products',
43 => 'Consumer_Consumer Staples_Household & Personal Products_Beauty & Cosmetics',
44 => 'Consumer_Consumer Staples_Household & Personal Products_Household Products',
45 => 'Consumer_Consumer Staples_Packaged Food & Beverage',
46 => 'Consumer_Consumer Staples_Packaged Food & Beverage_Beverage',
47 => 'Consumer_Consumer Staples_Packaged Food & Beverage_Beverage _Energy Drinks',
48 => 'Consumer_Consumer Staples_Packaged Food & Beverage_Packaged Food',
49 => 'Consumer_Consumer Staples_Packaged Food & Beverage_Packaged Food_Alternative Protein',
50 => 'Consumer_Consumer Staples_Tobacco / Cannabis',
51 => 'Consumer_Consumer Staples_Tobacco / Cannabis_Cannabis',
52 => 'Consumer_Consumer Staples_Tobacco / Cannabis_Tobacco',
json 格式非常理想,但对于所有条目:
{
'Consumer':{
'Consumer Discretionary':{
'Broadline Retail':'Broadline Retail',
'Consumer Services':{
'Child Care':'Child Care'
}
'Apparel, Footwear & Accessories':{
'Activewear':'Activewear',
'Apparel':'Apparel'
}
}
}
}
到目前为止,我在 php 中有这段代码
$sectors = array_values($subsectorTags);
$structure=[];
$firstLevel = getByLevel(1,$sectors);
$secondLevel = getByLevel(2,$sectors,$firstLevel);
$thirdLevel = getByLevel(3,$sectors,$secondLevel);
var_export($sectors);
function getByLevel($level,$sectors,$prevLevel=null){
$structure=[];
for($i = 0 ; $i<sizeof($sectors);$i++){
$parts = explode('_',$sectors[$i]);
if(sizeof($parts)==$level){
if($prevLevel && $level==2){
$prevLevel[$parts[$level-2]][]=$parts[$level-1];
}else if ($level==3){
// somehow here i need to implement a way to access and set the inner
// members of the structure with their children
$count = 0 ;
//$stuc = $prevLevel[$parts[$count]];
while($count<$level){
// $stuc= $stuc[$parts[$count]];
// $count=$count+1;
}
//$stuc[$parts[$count]][]=$parts[$count];
}else{
$structure[$sectors[$i]] = [];
}
}
}
return (!$prevLevel)?$structure:$prevLevel;
}
解决方案
作弊的一种方法是使用&
保持对您在_
角色爆炸时所处的“最深”级别的引用。在这里回复
// Our output array
$array = [];
foreach ($data as $item) {
// Store a reference to the "deepest" item (so far)
$last = &$array;
// Split on known delim and iterate over
foreach (explode('_', $item) as $key) {
// See next comment for what this does.
if (!is_array($last)) {
$last = [];
}
// Set a key to match the value exactly. If the value has "child" objects, the logic
// above will take care of removing the value and replacing it with an array.
if (!array_key_exists($key, $last)) {
$last[$key] = $key;
}
// Reset our "deepest" item
$last = &$last[$key];
}
}
这会产生:
{
"Business Services":
{
"Consulting": "Consulting",
"Foodservice Distribution": "Foodservice Distribution",
"Information Services": "Information Services"
},
"Consumer":
{
"Consumer Discretionary":
{
"Apparel, Footwear & Accessories":
{
"Activewear": "Activewear",
"Apparel": "Apparel",
"Footwear": "Footwear"
}
}
}
}
推荐阅读
- python - 如何保持第一次出现在python数据框中重复的值?
- c# - 使用 C# 连接 MySQL Server 8.0
- python - 如何将geopandas图提取为由像素数值组成的numpy数组?
- tensorflow - 如何将操作结果分配给 Layer.kernel?
- metaprogramming - 如何动态添加类属性?
- excel - 从列表框用户窗体中删除一行
- php - PHP Shell_exec = 如何在网页上显示的表格中输出结果?
- google-cloud-platform - 如何使用 stackdriver 从对话流中检索对话数据?
- laravel - 示例 laravel 项目 error500
- ruby-on-rails - Letter_opener 不再起作用的原因是什么?