You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP特定索引元素最大值计算及多有序数组逐索引选优算法问询

Hey there! Let's tackle your two PHP-related questions step by step:

1. PHP中计算特定索引元素最大值的算法

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 using array_column, then pass that list to max to 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 9
    

    Note: 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;
    
2. Multi-sorted array index-by-index filtering for the optimal array

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

  1. Start with all arrays as your initial candidate pool.
  2. 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.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:09:57