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

Analyze How Array Functions Affect Output for Identical Inputs Under Different Execution Orders: include and References

1. Reference: In Laravel 6, analysis of the differences in the same input after manipulating arrays using array-related functions under different execution orders (include, references).

2. The solution at the time: Replace $value with $sectionSettingValue to temporarily work around the variable name conflict caused by references.

3. Later, a similar issue arose, and I needed to quickly identify which variable name conflicts were causing the problem. I decided to print all variables at the beginning of the file where the execution error occurred. Finally, I would unset() all variables in the printed results that were not null.

PHP

\Illuminate\Support\Facades\Log::info(
    'schema.v2.1.80',
    [
        '$sectionKey' => $sectionKey ?? null,
        '$section' => $section ?? null,
        '$blockKey' => $blockKey ?? null,
        '$block' => $block ?? null,
        '$blockOrderKey' => $blockOrderKey ?? null,
        '$blocks' => $blocks ?? null,
    ]
);

4. The printed results are as follows. I confirmed that after the previous execution, the variables $sectionKey, $section, $blockKey, $block, $blockOrderKey, and $type were all not null and all needed to be destroyed.

Plaintext

[2023-09-04 10:43:45] local.INFO: schema.v2.1.80 {
    "$sectionKey": "image-1",
    "$section": {
        "type": "image",
        "disabled": false,
        "settings": {
            "heading": "",
            "heading_align": "center"
        },
        "blocks": {
            "column-0": {
                "type": "column",
                "settings": {
                    "image": "https://xxx-s3.s3.us-east-2.amazonaws.com/image/736589fac73c7dfdbf61efadb49a95fb9af16efb.jpeg",
                    "mobile_image": "https://cdn.xxx.com/image/2022/03/200062b82e8cbae7cf7c48b425a33260686fd4aa3e6ed656901cb4cc7a283487.jpeg",
                    "url": "/"
                }
            },
            "column-1": {
                "type": "column",
                "settings": {
                    "image": "https://xxx-s3.s3.us-east-2.amazonaws.com/image/736589fac73c7dfdbf61efadb49a95fb9af16efb.jpeg",
                    "mobile_image": "https://cdn.xxx.com/image/2022/03/200062b82e8cbae7cf7c48b425a33260686fd4aa3e6ed656901cb4cc7a283487.jpeg",
                    "url": "/"
                }
            }
        }
    },
    "$blockKey": "column-1",
    "$block": {
        "type": "column",
        "settings": {
            "image": "https://xxx-s3.s3.us-east-2.amazonaws.com/image/736589fac73c7dfdbf61efadb49a95fb9af16efb.jpeg",
            "mobile_image": "https://cdn.xxx.com/image/2022/03/200062b82e8cbae7cf7c48b425a33260686fd4aa3e6ed656901cb4cc7a283487.jpeg",
            "url": "/"
        }
    },
    "$blockOrderKey": 8,
    "$type": [
        "search",
        "all_collections"
    ]
} 

5. The code to destroy the variables is as follows:

PHP

unset($sectionKey, $section, $blockKey, $block, $blockOrderKey, $type);

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系