PHP特定索引元素最大值计算及多有序数组逐索引选优算法问询
Hey there! Let's tackle your two PHP-related questions step by step:
If you need to find the maximum value at a specific index across multiple arrays, there are a couple of straightforward approaches:
Method 1: Use
array_column+max
This is the cleanest way for most scenarios. First extract all values at your target index usingarray_column, then pass that list tomaxto get the highest value:$arrays = [ [1, 3, 6, 7], [1, 5, 7, 9], [1, 5, 9, 10] ]; $targetIndex = 2; // Remember PHP uses 0-based indexing, so this is the 3rd element $valuesAtTarget = array_column($arrays, $targetIndex); $maxValue = max($valuesAtTarget); echo $maxValue; // Outputs 9Note: This works best if all subarrays have the target index. If some might be missing, add a quick check to avoid warnings.
Method 2: Manual Loop for Edge Cases
If you need to handle missing indices or custom logic, iterate through each array and track the maximum value manually:$arrays = [/* Your array collection */]; $targetIndex = 2; $maxValue = PHP_INT_MIN; foreach ($arrays as $arr) { if (isset($arr[$targetIndex]) && $arr[$targetIndex] > $maxValue) { $maxValue = $arr[$targetIndex]; } } echo $maxValue;
PHP doesn't have a built-in function that exactly matches this logic, but implementing it is straightforward. The core idea is to iteratively narrow down your candidate arrays by comparing values at each index, keeping only the ones with the maximum value at that step until you're left with the optimal array(s).
Implementation Logic
- Start with all arrays as your initial candidate pool.
- For each index starting from 0:
- Find the maximum value at the current index across all candidates.
- Filter the candidate pool to keep only arrays that have this maximum value at the current index.
- If only one candidate remains, break early—you've found your answer.
- If multiple candidates are left after all indices are checked, they're all equally optimal.
Example Code (Matching Your Sample)
function findOptimalArray(array $arrays): array { $candidates = $arrays; // Assume all arrays are the same length; use the first array's length as the limit $lastIndex = count(reset($candidates)) - 1; for ($i = 0; $i <= $lastIndex; $i++) { if (count($candidates) === 1) { break; // No need to keep checking } // Get the highest value at current index $currentMax = max(array_column($candidates, $i)); // Filter candidates to keep only those with this max value $candidates = array_filter($candidates, function($arr) use ($i, $currentMax) { return $arr[$i] === $currentMax; }); // Reset array keys to avoid messy indexing later $candidates = array_values($candidates); } // Return the single optimal array, or all if there's a tie return count($candidates) === 1 ? $candidates[0] : $candidates; } // Test with your sample data $testArrays = [ [1, 3, 6, 7], [1, 5, 7, 9], [1, 5, 9, 10] ]; $optimalArray = findOptimalArray($testArrays); print_r($optimalArray); // Outputs: Array ( [0] => 1 [1] => 5 [2] => 9 [3] => 10 )
Quick Notes
- This relies on all input arrays being sorted (non-decreasing/non-increasing)—the logic only makes sense if later indices build on earlier ones.
- If arrays have different lengths, add a check upfront (e.g., only use arrays that match the shortest length, or handle missing indices explicitly).
- If ties are possible (multiple arrays remaining after all checks), the function returns all of them—adjust the return statement if you need to pick just one.
内容的提问来源于stack exchange,提问作者Mohammad Farfour

