PHP array formatting method and system, electronic equipment and storage medium
By introducing rule definition module, rule parser and formatting engine in PHP array formatting, using JSON format rule definition statements and declarative paradigms, the problems of poor flexibility and low efficiency in the existing technology are solved, and efficient and flexible array formatting are achieved.
Patent Information
- Application Number
- CN202411656359.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-19
- Publication Date
- 2025-04-25
AI Technical Summary
The existing PHP array formatting technology has poor flexibility, difficulty in dealing with complex structures and requirements changes, low efficiency, high maintenance, poor code readability and high modification cost.
The original PHP array is obtained through the rule definition module, and the rule definition statement is configured using JSON format based on the declarative paradigm. The parsing rule definition statement is converted into a format logical representation executable within the program, and the array is formatted using the formatting engine.
It realizes the flexibility and efficiency of PHP array formatting, and can freely customize complex formatting rules, simplify code writing and maintenance, reduce development and maintenance costs, and improve data processing performance.
Smart Images

Figure CN119886060A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software development, and in particular to a PHP array formatting method, apparatus, system, computer equipment, and computer-readable storage medium. Background Art
[0002] In the field of PHP development, arrays are the key to building the entire data storage and operation system, providing support for the orderly organization and efficient use of various types of data. Specifically, the concept of PHP array formatting is to accurately convert the data in the array according to specific rules or pre-set formats, because the formatted original array can better adapt to various demand scenarios such as intuitive display on the user interface, stable transmission in the network environment, and orderly storage in storage media such as databases.
[0003] In the related technology, the existing PHP array formatting technology has poor flexibility. When faced with complex structures and changing requirements, complex code combinations are required, and the efficiency is low. Multiple traversals and complex logic affect performance. At the same time, it is difficult to maintain, the code readability is poor, and the modification cost is high. Summary of the invention
[0004] The embodiments of the present application provide a PHP array formatting method, system, computer device and computer-readable storage medium to at least solve the problem of poor flexibility of PHP array formatting in the related art.
[0005] In a first aspect, an embodiment of the present application provides a PHP array formatting method, the method comprising:
[0006] Obtain the original PHP array through the rule definition module, and configure the rule definition statement in JSON format based on the declarative paradigm;
[0007] Parsing the rule definition statement through a rule parser, and converting the rule definition statement into a formatted logical representation executable within the program;
[0008] The formatting engine performs formatting processing on the PHP original array according to the formatting logic representation, and outputs a formatted array.
[0009] In some embodiments, formulating a rule definition statement based on a declarative paradigm includes:
[0010] Initialize the array formatting process by running the format public static function, wherein the format public static function is used to receive the PHP original array and pass a JSON string for defining formatting rules, wherein the JSON string is pre-written by the developer according to data formatting requirements;
[0011] The json_decode function is used to parse the JSON string into rule items, and the rule items are combined into a PHP associative array and stored in the rules rule array.
[0012] In some embodiments, in the process of parsing the JSON string into PH formatting rules, the method further includes:
[0013] In the case where the rule parameter in the JSON string is not passed or an error occurs in the passing, the JSON string is parsed into an object through the json_decode function, wherein the key-value pairs in the JSON string are mapped to attributes and attribute values of the object.
[0014] In some embodiments, parsing the rule definition statement by a rule parser and converting the rule definition language into a formatted logic representation executable by a program includes:
[0015] By calling recursive formatter formatRecursive, the PHP original array and the PHP associative array are passed to the rule parser;
[0016] By means of the rule parser, an empty array is created for storing formatted data;
[0017] Traverse the rules array, and extract the corresponding key information for each rule item;
[0018] The formatting logic is constructed according to the key information of each rule item, and the formatting logic of each rule item is combined and saved to obtain the formatting logic representation executable by the program, and the formatting logic representation is returned to the format public static function.
[0019] In some embodiments, the key information includes: key, new key, data type, filter, string case conversion requirement, and sub-rule,
[0020] The new key is pre-configured by the developer for each rule item and is used to rename the original array during the formatting process;
[0021] The filter is pre-configured by the developer for each rule item, and its value is a callable filter function, which is used to filter the data in the original data according to the preset rules;
[0022] The sub-rules are pre-configured by the developer for each rule item, and are used to define specific formatting rules for sub-elements or sub-fields in the nested data in the original array, so as to achieve refined formatting processing of complex nested structures.
[0023] In some embodiments, constructing formatting logic according to key information of each rule item includes:
[0024] Traverse the key information corresponding to each rule item to determine whether the rule item contains a key, a data type, a filter, case conversion logic, and a sub-rule;
[0025] In the case where the judgment result indicates that the key exists, an access logic supporting nested fields and arrays is defined according to the rule item, wherein the access logic is used to locate the original array to the corresponding field;
[0026] When the judgment result indicates that the data type exists, constructing corresponding data type conversion logic according to the value corresponding to the data type;
[0027] In the case where the judgment result indicates that the filter exists and is a callable function, constructing data filtering logic according to the filter function, wherein whether to retain the target data is determined according to the return value of any target function after passing through the filter function;
[0028] When the judgment result indicates that there is a requirement for converting the string case, constructing a string case conversion logic according to the configuration information of uppercase or lowercase in the rule item;
[0029] In the case where the judgment result indicates that the sub-rule exists, logic for recursively processing the nested data is constructed based on the sub-rule, and the formatting function is called again to process the nested data.
[0030] In a second aspect, an embodiment of the present application provides a PHP array formatting system, the system comprising: a rule definition module, a rule parser and a formatting engine, wherein:
[0031] The rule definition module is used to obtain the original PHP array and configure the rule definition statement in JSON format based on the declarative paradigm;
[0032] The rule parser is used to parse the rule definition statement and convert the rule definition statement into a formatted logical representation executable within the program;
[0033] The formatting engine is used to format the PHP original array according to the formatting logic representation and output the formatted array.
[0034] In some embodiments, the rule definition module is used to initialize the array formatting process by running the format public static function, wherein the format public static function is used to receive the PHP original array and pass a JSON string for defining the formatting rules, wherein the JSON string is pre-written by the developer according to the data formatting requirements;
[0035] The json_decode function is used to parse the JSON string into rule items, and the rule items are combined into a PHP associative array and stored in the rules rule array.
[0036] In a third aspect, an embodiment of the present application provides a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method described in the first aspect above when executing the computer program.
[0037] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described in the first aspect above.
[0038] Compared with the related art, the PHP array formatting method provided in the embodiment of the present application obtains the original PHP array, and based on the declarative paradigm, uses the JSON format to configure the rule definition statement; parses the rule definition statement, and converts the rule definition statement into a formatting logic representation that can be executed inside the program; formats the original PHP array according to the formatting logic representation, and outputs the formatted array. The defects of the traditional PHP array formatting, such as fixed rules, lack of flexibility, poor code readability, difficulty in processing complex data structures, and insufficient scalability, are solved. The method allows developers to freely customize complex formatting rules, makes the code intent clear and readable, facilitates the processing of nested data structures, and is easy to expand new functions. It can also efficiently process array data, reduce development and maintenance costs, and improve overall data processing performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0040] Figure 1 is a flow chart of a PHP array formatting method according to an embodiment of the present application;
[0041] Figure 2 is a flowchart of another PHP array formatting method according to an embodiment of the present application;
[0042] Figure 3 It is a structural block diagram of a PHP array formatting system according to an embodiment of the present application;
[0043] Figure 4 It is a schematic diagram of the internal structure of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0044] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application is described and illustrated below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application. Based on the embodiments provided in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application.
[0045] Obviously, the drawings described below are only some examples or embodiments of the present application. For ordinary technicians in this field, the present application can also be applied to other similar scenarios based on these drawings without creative work. In addition, it can also be understood that although the efforts made in this development process may be complicated and lengthy, for ordinary technicians in this field related to the content disclosed in this application, some changes in design, manufacturing or production based on the technical content disclosed in this application are just conventional technical means, and should not be understood as insufficient content disclosed in this application.
[0046] Reference to "embodiments" in this application means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those of ordinary skill in the art that the embodiments described in this application may be combined with other embodiments without conflict.
[0047] Unless otherwise defined, the technical terms or scientific terms involved in this application should be understood by people with ordinary skills in the technical field to which this application belongs. The words "one", "a", "a", "the" and the like involved in this application do not indicate a quantitative limitation, and may represent the singular or plural. The terms "include", "comprise", "have" and any of their variations involved in this application are intended to cover non-exclusive inclusions; for example, a process, method, system, product or device that includes a series of steps or modules (units) is not limited to the listed steps or units, but may also include steps or units that are not listed, or may also include other steps or units inherent to these processes, methods, products or devices. The words "connect", "connected", "coupled" and the like involved in this application are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. The "multiple" involved in this application refers to two or more. "And / or" describes the association relationship of associated objects, indicating that there may be three relationships, for example, "A and / or B" can represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the objects before and after are in an "or" relationship. The terms "first", "second", "third", etc. involved in this application are only used to distinguish similar objects and do not represent a specific ordering of the objects.
[0048] In the field of PHP development, arrays are the key to building the entire data storage and operation system, providing support for the orderly organization and efficient use of various types of data. PHP array formatting is to accurately convert the data in the array according to specific rules or pre-set formats. The formatted original array can better adapt to various demand scenarios such as intuitive display on the user interface, stable transmission in the network environment, and orderly storage in storage media such as databases.
[0049] The existing PHP array formatting generally involves the following technologies:
[0050] 1. Array functions: such as array_map(), array_filter(), array_reduce(), etc., which are used to map, filter, and reduce arrays.
[0051] 2. Array sorting: such as ˋsort()ˋ, ˋrsort()ˋ, ˋusort()ˋ, etc., used to sort array elements.
[0052] 3. Array key-value operations: such as array_keys(), array_values(), array_flip(), etc., which are used to operate array key-value pairs.
[0053] 4. Array combination and decomposition: such as array_merge(), array_slice(), array_chunk(), etc., used for array combination and decomposition.
[0054] Although PHP provides the above-mentioned rich array processing functions, the existing methods may appear to be inflexible or inefficient when faced with complex data structures and specific formatting requirements.
[0055] In view of this, the present application embodiment provides a PHP array formatting method. Figure 1 is a flow chart of a PHP array formatting method according to an embodiment of the present application, such as Figure 1 As shown, the process includes the following steps:
[0056] S101, obtain the PHP original array through the rule definition module, and configure the rule definition statement in JSON format based on the declarative paradigm;
[0057] An array is a data structure used to store a series of elements of the same type. These elements are accessed through indexes (also called subscripts), which are usually integers starting from 0. Arrays are basic and important data structures in many programming languages, allowing programmers to efficiently manage and manipulate large amounts of data.
[0058] In PHP, an array is a data structure that can store multiple values. These values can be integers, strings, floating point numbers, objects, or even another array (multidimensional array). Arrays in PHP are a very flexible and powerful tool that can greatly simplify the processing and management of data. In this embodiment, the PHP raw data is the array that will be formatted.
[0059] Specifically, the rule definition module provides developers with a clear entry point to describe array formatting rules. Based on the declarative paradigm, developers only need to focus on the formatting goals to be achieved rather than the specific implementation steps.
[0060] In addition, by using JSON format configuration rule definition statements, developers can express complex formatting requirements in a structured, easy-to-understand and easy-to-write way. For example, developers can define rules for various formatting operations such as data type conversion, key value renaming, conditional filtering, string processing, etc. in this module, and these rules all follow the JSON format syntax specifications.
[0061] In this module, the declarative paradigm is reflected in the fact that developers declare the expected format of data through concise JSON statements. For example, to convert the price field in an array to a floating-point type, developers only need to write {"key":"price","type":"float"} in the JSON rule without writing specific type conversion code logic. This approach greatly improves the efficiency and readability of rule definition, while also reducing the possibility of errors, because developers do not need to deeply understand the underlying implementation details, but only need to focus on expressing the formatting intention.
[0062] In addition, for computer systems, data in JSON format is easy to parse and process. In PHP, there are built-in functions (such as json_decode) that can easily convert rule definition strings in JSON format into data structures in PHP (such as arrays or objects), so that subsequent rule parsers can quickly read and understand the rule content and convert it into actual formatting logic. This format selection allows the rule definition to be seamlessly connected with the program's parsing and execution process, improving the efficiency and reliability of the entire array formatting process.
[0063] In an exemplary embodiment, step S102 specifically includes the following steps:
[0064] Step 1, initialize the array formatting process by running the format public static function, where the format public static function is used to receive the PHP original array and pass the JSON string that defines the formatting rules. The JSON string is pre-written by the developer according to the data formatting requirements;
[0065] Specifically, in a PHP program, to start formatting an array, you need to run a public static function called format. This function serves as the starting point for the entire array formatting process, and it is responsible for coordinating a series of subsequent formatting-related operations.
[0066] Furthermore, the format function has two key input parameters. The first is the PHP original array, which is the target data to be formatted. It contains various data elements that need to be sorted and converted according to specific rules.
[0067] Another JSON string that carries the defined formatting rules. These rules are pre-written by the developer based on specific data formatting requirements. For example, if you want to convert a field value in an array to a specific data type, convert the case of certain fields, or filter data based on specific conditions, the developer will describe these requirements in detail in this JSON string in the specified format.
[0068] Step 2, use the json_decode function to parse the JSON string into rule items and store the PHP associative array in the rules array.
[0069] In this embodiment, inside the format function, the PHP built-in json_decode function is used to process the received JSON string. The main function of this function is to convert the data in JSON format into a data structure that PHP can directly process.
[0070] When the json_decode function parses a JSON string, it converts it into a series of rule items in a specified way (usually setting the second parameter to true to parse it as an associative array). These rule items are organized into a PHP associative array and stored in the rules array variable of rules. Among them, the rules array becomes the specific basis for subsequent formatting operations. It clearly lists the details of each formatting rule, such as which operation is performed on which field, so that the original array can be formatted according to these rules later.
[0071] Through the above steps, the preliminary preparations for array formatting are completed, that is, the original data and formatting rules are received through the format function, and then the json_decode function is used to parse the JSON string of the rules into a rules array for subsequent operations, providing a data basis for subsequent formatting processing.
[0072] In addition, it should be noted that in the json_decode function, when parsing a JSON string, if the second parameter rule is not passed or false is passed, the json_decode function parses the JSON string into an object. In this case, the key-value pairs in the JSON data are directly mapped to the properties and property values of the object.
[0073] For example, for the JSON string {"name":"John","age":30}, after parsing, an object will be obtained, which has two attributes, name and age, with corresponding values of "John" and 30 respectively. This mapping method is more consistent with the structure of JSON data in some scenarios, especially when the processed data itself has a clear object hierarchy, it can more intuitively reflect the original structure of the data. In addition, when processing data according to rules, if the data is parsed into objects, the rules can directly operate on the attributes of the object.
[0074] S102, parsing the rule definition statement through the rule parser, and converting the rule definition statement into a formatted logical representation executable within the program;
[0075] Specifically, this step includes the following subdivision steps:
[0076] Step 1, pass the PHP original array and PHP associative array to the rule parser by calling the recursive formatting method;
[0077] In the array formatting process, the formatRecursive method is called. This method is designed to handle complex data structures and diverse formatting requirements. It accepts two important parameters, namely the PHP original array (containing the data to be formatted) and the PHP associative array obtained by parsing the rule definition statement through the json_decode function (storing detailed formatting rule items), and passes these two arrays to the rule parser;
[0078] Step 2, through the rule parser, create an empty array for storing formatted data;
[0079] After the rule parser receives the original array and associative array passed by the formatRecursive method, it first creates an empty array. The empty array is used to store formatted data. By creating this empty array in advance, it is prepared for the subsequent gradual filling of elements that conform to the formatting logic, ensuring that the data can be stored in an orderly manner according to the expected format, so that a complete and correctly formatted array can be finally returned.
[0080] Step 3, traverse the rules array and extract the corresponding key information for each rule item;
[0081] The rule parser traverses the passed in rules array. This rules array is obtained by parsing the rule definition statement previously, which contains multiple rule items, each of which corresponds to a specific formatting requirement. By traversing this array, each rule item can be processed in turn to ensure that no formatting rule is missed.
[0082] Furthermore, for each rule item in the rules array, the rule parser will extract the corresponding key information. This key information may include but is not limited to: the field key name to be operated (for example, if a specific field in the array is to be processed, the key name of the field needs to be clearly defined), the expected type conversion (such as converting the value of a field to an integer, floating point, etc.), whether conditional filtering is required (and the corresponding filter condition function), whether there are string processing requirements (such as case conversion, etc.), and whether nested data processing is involved (if so, the nested level and corresponding sub-rules need to be clearly defined). By accurately extracting this key information, the necessary materials are provided for the subsequent construction of formatting logic.
[0083] Specifically, the key information includes: obtaining the key (key), new key (new_key), data type (type), filter (filter), string case conversion requirements (uppercase or lowercase), and child rules (children) in the rule item;
[0084] Among them, the new key is pre-configured by the developer for each rule item, which is used to rename the original array during the formatting process;
[0085] New keys can be used with other rules such as type conversion. For example, in the case of $data = ['price'=>'123.45', 'count'=>'2'];, $rule = '[{"key":"price","type":"float","new_key":"formatted_price"},{"key":"count","type":"int","new_key":"quantity"}], not only will price be converted to float type and stored under the new key formatted_price, but count will also be converted to int type and stored under the new key quantity, and finally ['formatted_price'=>123.45,'quantity'=>2] will be obtained.
[0086] Application in nested structures: When processing nested data structures, the new key is also effective. For example, $data = ['user' = > ['name' = > 'Bob', 'age' = > 25]]; $rule = '[{"key":"user.name","new_key":"user_name"},{"key":"user.age","type":"int"}], after formatting, ['user_name' = > 'Bob', 'age' = > 25] is obtained, which realizes the renaming of keys when processing nested fields.
[0087] The filter is pre-configured by the developer for each rule item. Its value is a callable filter function, which is used to filter the data in the original data according to the preset rules. The developer can write a custom filter function according to the needs. For example, function customFilter($value){return strlen($value)>3;}, this function is used to filter strings with a length greater than 3. When this filter is applied, only string data that meets the length condition will be retained.
[0088] In addition, filters can also be used together with type conversion rules. For example, after the data is converted, the filter can be used to filter out the data that meets the specific conditions, and the data can be further processed accurately. When processing nested data structures, filters can also be applied to nested fields. For example, for a field in a nested array or object, a filter can be defined according to the rule to filter the value of the field to ensure that the data in the entire nested data structure meets the requirements.
[0089] Furthermore, sub-rules are pre-configured by developers for each rule item, and are used to define specific formatting rules for sub-elements or sub-fields in the nested data in the original array, so as to achieve refined formatting processing of complex nested structures.
[0090] When processing data with nested structures, sub-rules are recursively applied to the corresponding sub-elements or sub-fields in the nested data to achieve deep formatting of the nested data. This allows developers to define the formatting logic for complex data structures in a clear and structured way, ensuring that each part of the nested data can be formatted as expected.
[0091] Assume that there is original data $data = ['user' = > ['name' = > 'Bob', 'age' = > 25]], and rule $rule = '[{"key":"user","children":[{"key":"name","new_key":"user_name"},{"key":"age","type":"int"}]}]. During the formatting process, for the nested data of the user field, the sub-rule will rename the name field to user_name and convert the age field to int type. The final formatted data is ['user_name' = > 'Bob', 'age' = > 25].
[0092] It can be understood that sub-rules can work together with other rules (such as type conversion, new key definition, etc.) in nested data structures. For example, in the above example, the type conversion of the age field (type: "int") and the new key definition of the name field (new_key: "employee_name") are combined with the overall nested structure processing in the sub-rules to achieve comprehensive formatting of multi-layer nested data.
[0093] When processing data containing multiple nested levels and different types of nested structures (such as objects nested in arrays, arrays nested in objects, etc.), sub-rules can flexibly cooperate with other rules to format the data layer by layer according to the logic defined by the developer. For example, for a multi-layer nested data structure containing user information, order information, address information, etc., by reasonably defining sub-rules and other related rules, each part can be accurately formatted to meet various complex formatting requirements.
[0094] Step 4: Build formatting logic based on the key information of each rule item;
[0095] After extracting the key information of each rule item, the rule parser constructs the formatting logic for each rule item based on the key information.
[0096] S1, when the judgment result indicates that the key exists, define access logic supporting nested fields and arrays according to the rule item, wherein the access logic is used to locate the original array to the corresponding field;
[0097] For field selection, if the key exists, the data is located in the corresponding field according to its definition, which supports nested fields and array / object access logic. For example, if the key is user.name, the value of $data['user']['name'] is found through a series of operations.
[0098] S2, when the judgment result indicates that the data type exists, construct corresponding data type conversion logic according to the value corresponding to the data type;
[0099] According to the value of type, build the corresponding data type conversion logic.
[0100] In an exemplary embodiment, the specific implementation process is as follows:
[0101] $data=['price'=>'123.45','count'=>'2'];
[0102] $rule="[{"key":"price","type":"float"},{"key":"count","type":"int"}]';
[0103] From the value of the $rule variable, we know that the rule is to convert the string price '123.45' of the 'price' variable to the specified float type, and then convert the string quantity '2' of the 'count' variable to the specified int type; then call the format($data,$rule) method and the formatRecursive($data,$rules) method in the method to implement the process of converting data into a specific type. The actual output result is as follows:
[0104] ['price'=>123.45,'count'=>2]
[0105] S3, when the judgment result indicates that the filter exists and is a callable function, constructing data filtering logic according to the filter function, wherein whether to retain the target data is determined according to the return value of any target function filter function;
[0106] If filter exists and is a callable function, build the data filtering logic and decide whether to keep the data based on the return value of the filter function.
[0107] In an exemplary embodiment, the process specifically includes:
[0108] For example, to implement a filter that outputs strings with a length greater than 3, you can enter the parameters:
[0109]
[0110] In this embodiment, the specific customFilter filter is as follows:
[0111]
[0112] In addition, for example, if you want to output a prime number filter, you can enter the parameters like this:
[0113]
[0114] Implement the specially designed is_prime filter as follows:
[0115]
[0116] If the filter result is true, the original value is output, otherwise the null value is output; and 5 is a prime number, so the output result is: ['value'=>5].
[0117] S4, when the judgment result indicates that there is a requirement for string case conversion, construct string case conversion logic according to the configuration information of uppercase or lowercase in the rule item;
[0118] Specifically, according to the setting of uppercase or lowercase, the string case conversion logic is constructed, such as strtoupper($value) or strtolower($value). And according to the setting of uppercase or lowercase, the string case conversion logic is constructed, such as strtoupper($value) or strtolower($value).
[0119] S5, when the judgment result indicates that there are sub-rules, construct logic for recursively processing the nested data based on the sub-rules, and call the formatting function again to process the nested data.
[0120] If children exists, build the logic for recursively processing nested data and call the formatting function again to process the nested data.
[0121] For example, if a rule item is extracted that requires the value of a field to be converted to an integer type, then the corresponding logic will be built, which may involve using a suitable function (such as the filter_var function with the FILTER_VALIDATE_INT filter) to implement this type conversion operation; if a field is to be conditionally filtered, the corresponding filtering logic will be built based on the extracted filtering condition function. In this way, corresponding and executable formatting logic can be built in the rule parser for the specific formatting requirements of each rule item.
[0122] Step 5, save the formatting logic combination of each rule item;
[0123] In this embodiment, this combination operation is to integrate all formatting operations for different rule items to form a complete logical representation that can perform comprehensive formatting processing on the entire original array. By combining and saving various formatting logics, a formatting logic representation that can be executed by the program is obtained. This representation covers all the logic for processing each element and each field in the original array according to the requirements of all rule items, and can ensure that the data can be accurately formatted according to the predetermined format during subsequent execution.
[0124] Step 6, return the formatting logic representation to the format public static function.
[0125] Finally, the rule parser returns the constructed and combined program executable formatting logic representation to the format public static function. The format public static function can further format the original array according to the received formatting logic representation, and finally obtain a complete and qualified formatted array, which may be returned to the caller, thus completing the entire array formatting process.
[0126] S103, formatting the PHP original array according to the formatting logic representation through the formatting engine, and outputting the formatted array.
[0127] Among them, the formatting engine first needs to deeply understand the received formatting logic representation. It can be understood that this logical representation is generated by the rule parser after a series of operations such as parsing the rule definition statement, traversing the rule array to extract key information, and building and combining the formatting logic of each rule item. It completely covers the specific requirements and operation steps for formatting each element and each field in the original array, such as which fields need type conversion, which need conditional filtering, whether there is string processing, and how to handle nested data.
[0128] Based on the understanding of the formatting logic, the formatting engine begins to traverse each element in the PHP original array. For each element, it performs the corresponding formatting operation according to the specific rules for the field or data structure where the element is located in the logical representation.
[0129] After that, the formatting engine will output the processed array as the final result. This output array is the result of comprehensive formatting, and its elements and fields have been processed accordingly according to pre-defined rules. Whether it is data type, data screening, string format or nested data structure, it meets the expected formatting requirements, so as to meet the subsequent specific needs in data display, transmission, storage, etc.
[0130] Specifically, the array formatting process corresponding to the above steps S101 to S103 can be implemented by the following code:
[0131]
[0132]
[0133]
[0134]
[0135]
[0136] Figure 2 It is a flowchart of another PHP array formatting method according to an embodiment of the present application.
[0137] Through the above steps S101 to S103, by designing a rule definition language based on the declarative paradigm (presented in JSON format), developers are allowed to freely combine multiple formatting operation elements, and at the same time create a rule parser that can accurately parse this language, and a formatting engine that can efficiently perform formatting tasks based on the parsing results. The rule parser can identify various elements in the rule, such as type conversion, key value operation, conditional filtering, string processing and nested structure related rules, and convert them into logic executable by the formatting engine. The formatting engine can flexibly process data of different data types and complex nested structures to achieve deep and accurate formatting. These means solve the defects of fixed rules, lack of flexibility, poor code readability, difficulty in processing complex data structures and insufficient extensibility in traditional PHP array formatting. It has reached the effect of allowing developers to freely customize complex formatting rules, making the code intention clear and readable, conveniently processing nested data structures, and easy to expand new functions, and efficiently processing array data, reducing development and maintenance costs, and improving overall data processing performance.
[0138] It should be noted that the steps shown in the above process or the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0139] This embodiment also provides a PHP array formatting system, which is used to implement the above embodiments and preferred implementations, and will not be repeated here. As used below, the terms "module", "unit", "subunit", etc. can implement a combination of software and / or hardware for a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0140] Figure 3 is a structural block diagram of a PHP array formatting system according to an embodiment of the present application, such as Figure 3 As shown, the system includes a rule definition module 30, a rule parser 31 and a formatting engine 32, wherein:
[0141] The rule definition module 30 is used to obtain the original PHP array and configure the rule definition statement using the JSON format based on the declarative paradigm;
[0142] The rule parser 31 is used to parse the rule definition statement and convert the rule definition statement into a formatted logical representation executable within the program;
[0143] The formatting engine 32 is used to format the PHP original array according to the formatting logic representation and output the formatted array.
[0144] Table 1 is a specific example table of formatting a portion of a PHP array according to an embodiment of the present application, as shown in Table 1 below, and the specific examples include but are not limited to:
[0145] Example Input Instance Parameters Expected Output Result Actual Output Result Number
[0146]
[0147]
[0148] Example 1: Verifies the formatting of basic fields, including renaming keys and type conversion.
[0149] Example 2: Verified the extraction and formatting of nested fields to ensure that nested structures can be handled correctly.
[0150] Example 3: Verified the type conversion from string to floating point number and integer.
[0151] Example 4: Verified the functionality of using a conditional filter (in this case, email validation).
[0152] Example 5: Verified the function of converting a string to uppercase.
[0153] Example 6: Verifies the handling of type conversion when the input is null to ensure that no exception is thrown.
[0154] Example 7: Verifies the formatting of each element in the array to ensure that the rules are correctly applied to each sub-array.
[0155] Example 8: Verified the formatting of object properties to ensure that object properties can be processed like arrays.
[0156] Example 9: Verified the formatting of multi-level nested structures to ensure that deeply nested fields can be handled correctly.
[0157] Example 10: Verified the application of multiple conditional filters to ensure that multiple filters can be used simultaneously.
[0158] Example 11: Verified the application of custom filter functions to ensure that data can be filtered according to custom logic.
[0159] Example 12: Verified the handling of empty array input to ensure that no exception is thrown or incorrect results are returned.
[0160] Example 13: Verifies that when a rule references a non-existent key, the formatting result is correct or an exception is thrown.
[0161] Through the above system, the rule definition language based on the declarative paradigm allows developers to freely combine multiple formatting operation elements, and at the same time create a rule parser that can accurately parse this language, and a formatting engine that can efficiently perform formatting tasks based on the parsing results. The rule parser can identify various elements in the rules, such as type conversion, key-value operations, conditional filtering, string processing, and nested structure-related rules, and convert them into logic executable by the formatting engine. The formatting engine can flexibly process data of different data types and complex nested structures to achieve deep and accurate formatting processing. These means solve the defects of fixed rules, lack of flexibility, poor code readability, difficulty in processing complex data structures, and insufficient scalability in traditional PHP array formatting. It allows developers to freely customize complex formatting rules, make code intent clear and readable, facilitate processing of nested data structures, and easily expand new functions. It can also efficiently process array data, reduce development and maintenance costs, and improve overall data processing performance.
[0162] In one embodiment, a computer device is provided, which may be a terminal. The computer device includes a processor, a memory, a network interface, a display screen, and an input device connected via a system bus. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, a method for changing the costume of a virtual character is implemented. The display screen of the computer device may be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device may be a touch layer covered on the display screen, or a key, trackball, or touchpad provided on the housing of the computer device, or an external keyboard, touchpad, or mouse, etc.
[0163] In one embodiment, Figure 4 is a schematic diagram of the internal structure of an electronic device according to an embodiment of the present application, such as Figure 4 As shown, an electronic device is provided, which may be a server, and its internal structure diagram may be as shown in Figure 4 As shown. The electronic device includes a processor, a memory and a network interface connected through a system bus. Among them, the processor of the electronic device is used to provide computing and control capabilities. The memory of the electronic device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the electronic device is used to store data. The network interface of the electronic device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a PHP array formatting method is implemented.
[0164] Those skilled in the art will understand that Figure 4 The structure shown in the figure is merely a block diagram of a partial structure related to the scheme of the present application, and does not constitute a limitation on the electronic device to which the scheme of the present application is applied. The specific electronic device may include more or fewer components than shown in the figure, or combine certain components, or have a different arrangement of components.
[0165] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0166] Those skilled in the art should understand that the technical features of the above embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0167] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A PHP array formatting method, characterized in that: The method comprises: Obtain the original PHP array through the rule definition module, and configure the rule definition statement in JSON format based on the declarative paradigm; Parsing the rule definition statement through a rule parser, and converting the rule definition statement into a formatted logical representation executable within the program; The formatting engine performs formatting processing on the PHP original array according to the formatting logic representation, and outputs a formatted array.
2. The method according to claim 1, characterized in that Rule definition statements based on the declarative paradigm include: Initialize the array formatting process by running the format public static function, wherein the format public static function is used to receive the PHP original array and pass a JSON string for defining formatting rules, wherein the JSON string is pre-written by the developer according to data formatting requirements; The json_decode function is used to parse the JSON string into rule items, and the rule items are combined into a PHP associative array and stored in the rules rule array.
3. The method according to claim 2, characterized in that In the process of parsing the JSON string into PH formatting rules, the method further includes: In the case where the rule parameter in the JSON string is not passed or an error occurs in the passing, the JSON string is parsed into an object through the json_decode function, wherein the key-value pairs in the JSON string are mapped to attributes and attribute values of the object.
4. The method according to claim 2, characterized in that: Parsing the rule definition statement by a rule parser and converting the rule definition language into a formatted logical representation executable by a program includes: By calling recursive formatter formatRecursive, the PHP original array and the PHP associative array are passed to the rule parser; By means of the rule parser, an empty array is created for storing formatted data; Traverse the rules array, and extract the corresponding key information for each rule item; The formatting logic is constructed according to the key information of each rule item, and the formatting logic of each rule item is combined and saved to obtain the formatting logic representation executable by the program, and the formatting logic representation is returned to the format public static function.
5. The method according to claim 4, characterized in that The key information includes: key, new key, data type, filter, string case conversion requirements and sub-rule. The new key is pre-configured by the developer for each rule item and is used to rename the original array during the formatting process; The filter is pre-configured by the developer for each rule item, and its value is a callable filter function, which is used to filter the data in the original data according to the preset rules; The sub-rules are pre-configured by the developer for each rule item, and are used to define specific formatting rules for sub-elements or sub-fields in the nested data in the original array, so as to achieve refined formatting processing of complex nested structures.
6. The method according to claim 5, characterized in that Constructing formatting logic based on key information of each rule item includes: Traverse the key information corresponding to each rule item to determine whether the rule item contains a key, a data type, a filter, case conversion logic, and a sub-rule; In the case where the judgment result indicates that the key exists, an access logic supporting nested fields and arrays is defined according to the rule item, wherein the access logic is used to locate the original array to the corresponding field; When the judgment result indicates that the data type exists, constructing corresponding data type conversion logic according to the value corresponding to the data type; In the case where the judgment result indicates that the filter exists and is a callable function, constructing data filtering logic according to the filter function, wherein whether to retain the target data is determined according to the return value of any target function after passing through the filter function; When the judgment result indicates that there is a requirement for converting the string to uppercase and lowercase, constructing a string case conversion logic according to the configuration information of uppercase or lowercase in the rule item; In the case where the judgment result indicates that the sub-rule exists, logic for recursively processing the nested data is constructed based on the sub-rule, and the formatting function is called again to process the nested data.
7. A PHP array formatting system, characterized in that: include: Rule definition module, rule parser and formatting engine, where: The rule definition module is used to obtain the original PHP array and configure the rule definition statement in JSON format based on the declarative paradigm; The rule parser is used to parse the rule definition statement and convert the rule definition statement into a formatted logical representation executable within the program; The formatting engine is used to format the PHP original array according to the formatting logic representation and output the formatted array.
8. The system according to claim 7, characterized in that The rule definition module is used to initialize the array formatting process by running the format public static function, wherein the format public static function is used to receive the PHP original array and pass a JSON string for defining the formatting rules, wherein the JSON string is pre-written by the developer according to the data formatting requirements; The json_decode function is used to parse the JSON string into rule items, and the rule items are combined into a PHP associative array and stored in the rules rule array.
9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 6 is implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.