PHP字符串截取后入数组及从stdClass提取规则到指定数组索引
Got it, let's break down these two PHP tasks step by step!
需求1:字符串截取部分字符后添加到数组
This is a common task, and PHP's substr() function makes it straightforward. Here's a practical example that shows how to slice a string and add the result to an existing array:
// Example original string $originalString = "HelloStackOverflow"; // Grab the first 5 characters (start at index 0, take 5 characters) $slicedString = substr($originalString, 0, 5); // Target array we want to add to $targetArray = ["existing_item_1", "existing_item_2"]; // Add the sliced string to the array (two ways to do this) $targetArray[] = $slicedString; // Quickest method // OR: array_push($targetArray, $slicedString); // Check the result print_r($targetArray);
Notes:
substr($string, $start, $length)works by:$start: The index to start slicing from (0 = beginning, negative numbers count from the end of the string)$length: Optional number of characters to take (omit it to grab everything from$startto the end)
- Adjust the
$startand$lengthvalues to match exactly which part of the string you need.
需求2:转换stdClass对象结构到新数组的
rules索引 For this task, we need to group the rules_field_N, rules_condition_N, and rules_value_N properties from your stdClass object into nested arrays under $newdata['rules']. The trick is to spot the numeric suffix in each property name and group related fields by that number.
Here's a working implementation:
// Your sample stdClass object $data = new stdClass(); $data->class = 'srt-fields'; $data->rules_field_1 = 1; $data->rules_condition_1 = 0; $data->rules_value_1 = 'text'; $data->rules_field_2 = 3; $data->rules_condition_2 = 1; $data->rules_value_2 = 'another_text'; // Initialize the new array $newdata = []; $newdata['rules'] = []; // Loop through all properties in the stdClass object foreach ($data as $key => $value) { // Skip the 'class' property since we don't need it for rules if ($key === 'class') continue; // Split the property name into parts (e.g., 'rules_field_1' becomes ['rules', 'field', '1']) $keyParts = explode('_', $key); $ruleIndex = end($keyParts); // Grab the numeric suffix (1, 2, etc.) $fieldType = $keyParts[1]; // Grab the field type (field, condition, value) // Initialize the rule array if it doesn't exist yet if (!isset($newdata['rules'][$ruleIndex])) { $newdata['rules'][$ruleIndex] = []; } // Assign the value to the correct field in the rule array $newdata['rules'][$ruleIndex][$fieldType] = $value; } // Re-index the rules array to get continuous numeric keys (0, 1, 2...) instead of 1, 2 $newdata['rules'] = array_values($newdata['rules']); // Check the final structure print_r($newdata['rules']);
What this does:
- Skips the
classproperty since it's not part of the rules - Splits each rule-related property name to extract the numeric index and field type
- Groups all fields with the same numeric index into a single array
- Uses
array_values()to reset the array keys so they're sequential (optional, but matches your desired output structure)
内容的提问来源于stack exchange,提问作者Sanjeev Aryal
相关产品推荐
相关产品推荐

