PHP实现下划线分隔层级数组转换为多层JSON结构
实现方案
核心思路
不用按层级拆分多次遍历数组,直接遍历每个层级字符串,通过引用指针动态定位到嵌套结构的对应层级,自动创建中间节点,最后一层节点直接赋值为同名字符串即可,天然支持任意深度的层级,无需硬编码层级判断逻辑。
完整可运行代码
// 替换为你实际的数据源 $subsectorTags = [ 'Business Services', 'Business Services_Consulting', 'Business Services_Foodservice Distribution', 'Business Services_Information Services', 'Consumer', 'Consumer_Consumer Discretionary', 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories', 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Activewear', 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Apparel', 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Footwear', 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Luxury Goods', 'Consumer_Consumer Discretionary_Broadline Retail', 'Consumer_Consumer Discretionary_Consumer Services', 'Consumer_Consumer Discretionary_Consumer Services_Child Care', 'Consumer_Consumer Discretionary_Consumer Services_Fitness Services/Gyms', 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure', 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Cruise Lines', 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Gaming', 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Live Entertainment', 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Lodging', 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Online Sports Betting', 'Consumer_Consumer Discretionary_Gaming, Lodging & Leisure_Theme Parks', 'Consumer_Consumer Discretionary_Recreational Products', 'Consumer_Consumer Discretionary_Recreational Products_Fitness Equipment', 'Consumer_Consumer Discretionary_Recreational Products_Recreational Vehicles', 'Consumer_Consumer Discretionary_Recreational Products_Sporting Goods - Recreational Products', 'Consumer_Consumer Discretionary_Recreational Products_Toys', 'Consumer_Consumer Discretionary_Restaurants', 'Consumer_Consumer Discretionary_Restaurants_Casual Dining', 'Consumer_Consumer Discretionary_Restaurants_Fast Casual', 'Consumer_Consumer Discretionary_Restaurants_Full-Service Dining', 'Consumer_Consumer Discretionary_Restaurants_Ghost Kitchen', 'Consumer_Consumer Discretionary_Restaurants_QSR', 'Consumer_Consumer Discretionary_Specialty Retail', 'Consumer_Consumer Discretionary_Specialty Retail_Auto Retail', 'Consumer_Consumer Discretionary_Specialty Retail_Auto Retail_Online Auto Marketplaces', 'Consumer_Consumer Discretionary_Specialty Retail_Consumer Electronics', 'Consumer_Consumer Discretionary_Specialty Retail_Food Retail', 'Consumer_Consumer Discretionary_Specialty Retail_Home Furnishings', 'Consumer_Consumer Discretionary_Specialty Retail_Home Improvement', 'Consumer_Consumer Discretionary_Specialty Retail_Sporting Goods - Specialty Retail', 'Consumer_Consumer Staples', 'Consumer_Consumer Staples_Household & Personal Products', 'Consumer_Consumer Staples_Household & Personal Products_Beauty & Cosmetics', 'Consumer_Consumer Staples_Household & Personal Products_Household Products', 'Consumer_Consumer Staples_Packaged Food & Beverage', 'Consumer_Consumer Staples_Packaged Food & Beverage_Beverage', 'Consumer_Consumer Staples_Packaged Food & Beverage_Beverage _Energy Drinks', 'Consumer_Consumer Staples_Packaged Food & Beverage_Packaged Food', 'Consumer_Consumer Staples_Packaged Food & Beverage_Packaged Food_Alternative Protein', 'Consumer_Consumer Staples_Tobacco / Cannabis', 'Consumer_Consumer Staples_Tobacco / Cannabis_Cannabis', 'Consumer_Consumer Staples_Tobacco / Cannabis_Tobacco', ]; $structure = []; foreach ($subsectorTags as $item) { $parts = explode('_', $item); $totalLevel = count($parts); // 用引用跟踪当前操作的嵌套层级 $currentNode = &$structure; foreach ($parts as $level => $nodeName) { // 最后一级直接赋值为 键名:值 同名 if ($level === $totalLevel - 1) { $currentNode[$nodeName] = $nodeName; break; } // 中间级不存在则初始化空数组 if (!isset($currentNode[$nodeName])) { $currentNode[$nodeName] = []; } // 移动指针到下一级 $currentNode = &$currentNode[$nodeName]; } // 销毁引用避免后续循环污染数据 unset($currentNode); } // 打印符合要求的JSON格式 echo json_encode($structure, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
方案优势
- 无层级限制:支持任意深度的下划线分隔层级,不需要提前知道层级数量,也不需要为不同层级写单独的处理逻辑
- 执行效率高:仅需要遍历一次原始数组,时间复杂度为O(N)(N为所有字符串的总字符数),远高于按层级多次遍历的方案
- 代码简洁:逻辑清晰无冗余,维护成本极低
内容的提问来源于stack exchange,提问作者arl
相关产品推荐
相关产品推荐

