没有不值得去解决的问题,也没有不值得去学习的技术!

在 Laravel 6 中,基于 数组相关函数 操作数组后,同样的输入,在不同执行顺序下的差异的分析(include、引用)

但是,返回的 $schema ,即 [2023-05-17 09:45:23] local.INFO: v2.1.3_migrate_index.php_schema_1 ,不符合预期。其数据有丢失。在 sections.4imagetext_t0upqwh3 中的字段:type、settings、block_order 等皆已经丢失。

1、PHP 文件的代码实现如下

<?php
 
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
 
Log::info(
    'v2.1.3_migrate_index.php_schema_0',
    [
        'schema' => $schema
    ]
);
 
$settings = ['text_color', 'mask_opacity', 'button_text_color', 'button_background_color'];
if (Arr::has($schema, 'sections')) {
    foreach ($schema['sections'] as $sectionKey => $section) {
        if (Arr::get($section, 'type') == 'multi-column') {
            foreach ($settings as $setting) {
                if (Arr::has($section, 'settings.' . $setting)) {
                    if ($setting == 'mask_opacity') {
                        $value = (bool)Arr::get($section, 'settings.' . $setting);
                    } else {
                        $value = Arr::get($section, 'settings.' . $setting);
                    }
                    foreach ($section['blocks'] as $blockKey => $block) {
                        Arr::set($schema, 'sections.' . $sectionKey . '.blocks.' . $blockKey . '.settings.' . $setting, $value);
                    }
                    Arr::forget($schema, 'sections.' . $sectionKey . '.settings.' . $setting);
                }
            }
        }
    }
}
 
Log::info(
    'v2.1.3_migrate_index.php_schema_1',
    [
        'schema' => $schema
    ]
);
 
return $schema;

2、初始的 $schema ,即 [2023-05-17 09:45:23] local.INFO: v2.1.3_migrate_index.php_schema_0 ,其值如下


{
  "schema": {
    "sections": {
      "4imagetext_t0upqwh3": {
        "type": "multi-column",
        "disabled": false,
        "settings": {
          "heading": "",
          "heading_align": "center",
          "text_color": "#000",
          "image_size": "auto",
          "mask_opacity": 0,
          "button_text_color": "#000",
          "button_background_color": "#fff",
          "text_align": "center"
        },
        "blocks": {
          "column-0": {
            "type": "column",
            "settings": {
              "heading": "IMAGE WITH TEXT",
              "text": "Use these blocks to share your brand's story. You can focus on a particular collection, a product detail, or even embed a video.",
              "image": "https://cdn.cloudfastin.com/image/cafe2c8e6451a85f66aedbc62af22747f711c3f2.jpeg",
              "mobile_image": "",
              "url": "/",
              "label": "MORE INFO"
            }
          },
          "column-1": {
            "type": "column",
            "settings": {
              "heading": "IMAGE WITH TEXT",
              "text": "Use these blocks to share your brand's story. You can focus on a particular collection, a product detail, or even embed a video.",
              "image": "https://cdn.cloudfastin.com/image/b55803eeb93eba8bb0b8bc4bb4be7ad16df74ad1.jpeg",
              "mobile_image": "",
              "url": "/",
              "label": "MORE INFO"
            }
          },
          "column-2": {
            "type": "column",
            "settings": {
              "heading": "IMAGE WITH TEXT",
              "text": "Use these blocks to share your brand's story. You can focus on a particular collection, a product detail, or even embed a video.",
              "image": "https://cdn.cloudfastin.com/image/083a9e7fbe2b3856aecb84a2387f82eea0bb0f39.jpeg",
              "mobile_image": "",
              "url": "/",
              "label": "MORE INFO"
            }
          }
        },
        "block_order": [
          "column-0",
          "column-1",
          "column-2"
        ]
      }
    },
    "order": [
      "4imagetext_t0upqwh3"
    ]
  }
}


3、但是,返回的 $schema ,即 [2023-05-17 09:45:23] local.INFO: v2.1.3_migrate_index.php_schema_1 ,不符合预期。其数据有丢失。在 sections.4imagetext_t0upqwh3 中的字段:type、settings、block_order 等皆已经丢失。如图1

但是,返回的 $schema ,即 [2023-05-17 09:45:23] local.INFO: v2.1.3_migrate_index.php_schema_1 ,不符合预期。其数据有丢失。在 sections.4imagetext_t0upqwh3 中的字段:type、settings、block_order 等皆已经丢失。
图1

{
    "schema": {
        "sections": {
            "4imagetext_t0upqwh3": {
                "blocks": {
                    "column-0": {
                        "settings": {
                            "button_background_color": "#fff"
                        }
                    },
                    "column-1": {
                        "settings": {
                            "button_background_color": {
                                "blocks": {
                                    "column-0": {
                                        "settings": {
                                            "button_background_color": "#fff"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "column-2": {
                        "settings": {
                            "button_background_color": {
                                "blocks": {
                                    "column-0": {
                                        "settings": {
                                            "button_background_color": "#fff"
                                        }
                                    },
                                    "column-1": {
                                        "settings": {
                                            "button_background_color": {
                                                "blocks": {
                                                    "column-0": {
                                                        "settings": {
                                                            "button_background_color": "Over 15 levels deep, aborting normalization"
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        "order": [
            "4imagetext_t0upqwh3"
        ]
    }
} 


4、但是,仅执行此 PHP 文件,返回的 $schema 是符合预期的。第 3 步骤的执行不符合预期,根源应该在于执行此 PHP 文件之前的某处影响到了这个 PHP 文件的执行。


{
    "schema": {
        "schema": {
            "order": [
                "4imagetext_t0upqwh3"
            ],
            "sections": {
                "4imagetext_t0upqwh3": {
                    "type": "multi-column",
                    "blocks": {
                        "column-0": {
                            "type": "column",
                            "settings": {
                                "url": "/",
                                "text": "Use these blocks to share your brand's story. You can focus on a particular collection, a product detail, or even embed a video.",
                                "image": "https://cdn.cloudfastin.com/image/cafe2c8e6451a85f66aedbc62af22747f711c3f2.jpeg",
                                "label": "MORE INFO",
                                "heading": "IMAGE WITH TEXT",
                                "mobile_image": ""
                            }
                        },
                        "column-1": {
                            "type": "column",
                            "settings": {
                                "url": "/",
                                "text": "Use these blocks to share your brand's story. You can focus on a particular collection, a product detail, or even embed a video.",
                                "image": "https://cdn.cloudfastin.com/image/b55803eeb93eba8bb0b8bc4bb4be7ad16df74ad1.jpeg",
                                "label": "MORE INFO",
                                "heading": "IMAGE WITH TEXT",
                                "mobile_image": ""
                            }
                        },
                        "column-2": {
                            "type": "column",
                            "settings": {
                                "url": "/",
                                "text": "Use these blocks to share your brand's story. You can focus on a particular collection, a product detail, or even embed a video.",
                                "image": "https://cdn.cloudfastin.com/image/083a9e7fbe2b3856aecb84a2387f82eea0bb0f39.jpeg",
                                "label": "MORE INFO",
                                "heading": "IMAGE WITH TEXT",
                                "mobile_image": ""
                            }
                        }
                    },
                    "disabled": false,
                    "settings": {
                        "heading": "",
                        "image_size": "auto",
                        "text_align": "center",
                        "text_color": "#000",
                        "mask_opacity": 0,
                        "heading_align": "center",
                        "button_text_color": "#000",
                        "button_background_color": "#fff"
                    },
                    "block_order": [
                        "column-0",
                        "column-1",
                        "column-2"
                    ]
                }
            }
        }
    }
}


5、最终确认了原因,是因为在执行此 PHP 文件之前,执行了另一个 PHP 文件,在另一个 PHP 文件中使用了引用:&$value。两个 PHP 文件的依次执行是使用的 include 。

<?php
 
use Illuminate\Support\Arr;
 
if (Arr::has($schema, 'sections'))
{
    foreach ($schema['sections'] as &$value) {
        // ...
    }
}
 
return $schema;

6、将 $value 替换为 $sectionSettingValue,临时规避掉引用的变量名重名的问题。

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理