There is no problem not worth solving, and no technology not worth learning!

In Laravel 6, after the array is operated based on the array-related function, the same input, the analysis of the differences under different execution orders (include, reference)

但是,返回的 $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. The code implementation of the PHP file is as follows

<?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. The initial $schema , that is[2023-05-17 09:45:23]local.info: v2.1.3_migrate_index.php_schema_0 , its values are as follows

{
  "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, however, the returned $schema , i.e.[2023-05-17 09:45:23]local.info: v2.1.3_migrate_index.php_schema_1 , not as expected. Its data is lost. The fields in sections.4imagetext_t0upqwh3 have been lost. as shown in Figure 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 等皆已经丢失。
Figure 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. However, only executing this php file, the returned $schema is as expected. The execution of the third step is not as expected, and the root cause should be that somewhere before the execution of this PHP file affects the execution of this PHP file.

{
    "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. The reason was finally confirmed, because before executing this php file, another php file was executed, and the reference was used in another php file: &$value. The sequential execution of the two PHP files is to use include .

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

6. Replace $Value with $SectionSettingValue, and temporarily avoid the problem of the variable name with the same name.

PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.