-8

我有 3 个数组

阵列 1:

 Array ( [0] => 0 [1] => 256 [2] => 512 [3] => 768 [4] => 1024 [5] => 1280 [6] => 1536 [7] => 1792 [8] => 2048 [9] => 2304 [10] => 2560 [11] => 2816 [12] => 3072 [13] => 3328 [14] => 3584 [15] => 3840 [16] => 4096 [17] => 4352 [18] => 4608 [19] => 4864 [20] => 5120 [21] => 5376 [22] => 5632 [23] => 5888 )

阵列 2:

Array ( [0] => 0 [1] => 65536 [2] => 131072 [3] => 196608 [4] => 262144 [5] => 327680 [6] => 393216 [7] => 458752 [8] => 524288 [9] => 589824 [10] => 655360 [11] => 720896 [12] => 786432 [13] => 851968 [14] => 917504 [15] => 983040 [16] => 1048576 [17] => 1114112 [18] => 1179648 [19] => 1245184 [20] => 1310720 [21] => 1376256 [22] => 1441792 [23] => 1507328 [24] => 1572864 [25] => 1638400 [26] => 1703936 [27] => 1769472 [28] => 1835008 [29] => 1900544 [30] => 1966080 [31] => 2031616 )

阵列 3:

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 [16] => 16 [17] => 17 [18] => 18 [19] => 19 [20] => 20 [21] => 21 [22] => 22 [23] => 23 [24] => 24 [25] => 25 [26] => 26 [27] => 27 [28] => 28 [29] => 29 [30] => 30 [31] => 31 )

给定一个我知道的数字 X 是a1[i] + a2[j] + a3[k]如何计算的ij并且k

例子:

  1. X = 458752 => i=0, j=7,k=0即 458752 = 0 + 458752 + 0
  2. X = 131586 => i=2, j=2,k=2即 131586 = 256 + 131072 + 2
  3. X = 65793 => i=1, j=1,k=1即 65793 = 256 + 65536 + 1
4

3 回答 3

0

$value = $array1[0]+$array1[7]+$array1[0]

应该管用。

数组索引从 0 开始,从$array[1]技术上讲,数组中的第二个元素也是如此。

于 2013-01-22T04:57:37.450 回答
0

怎么样:

foreach(array(458752, 131586, 65793) as $x) {
    $j = intval($x / 65536);
    $x = $x % 65536;
    $i = intval($x / 256);
    $k = $x % 256;
    echo "x=$x, i=$i, j=$j, k=$k\n";
}

输出:

x=0, i=0, j=7, k=0
x=514, i=2, j=2, k=2
x=257, i=1, j=1, k=1
于 2013-01-22T11:23:50.053 回答
-1

首先,您尝试清楚地发布您的问题。

您的第一个数组计数与其他两个数组不匹配。因此,如果 3 个数组的数量必须相等,那么您可以轻松获得这些值。如果您需要自定义加法值,请对数组值进行硬编码并尝试加法。

方法 1:所有 3 个数组计数相同。

<?php
$first = Array (0 => 0, 1 => 256, 2 => 512, 3 => 768, 4 => 1024, 5 => 1280, 6 => 1536, 7 => 1792, 8 => 2048, 9 => 2304, 10 => 2560, 11 => 2816, 12 => 3072, 13 => 3328, 14 => 3584, 15 => 3840, 16 => 4096, 17 => 4352, 18 => 4608, 19 => 4864, 20 => 5120, 21 => 5376, 22 => 5632, 23 => 5888 );

$second =  Array (0 => 0, 1 => 65536, 2 => 131072, 3 => 196608, 4 => 262144, 5 => 327680, 6 => 393216, 7 => 458752, 8 => 524288, 9 => 589824, 10 => 655360, 11 => 720896, 12 => 786432, 13 => 851968, 14 => 917504, 15 => 983040, 16 => 1048576, 17 => 1114112, 18 => 1179648, 19 => 1245184, 20 => 1310720, 21 => 1376256, 22 => 1441792, 23 => 1507328);

$third = Array (0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 16 => 16, 17 => 17, 18 => 18, 19 => 19, 20 => 20, 21 => 21, 22 => 22, 23 => 23);

$result = array();
foreach($first as $key => $values){
        echo '<br>'.$values.'+'.$second[$key].'+'.$third[$key];
    $result[] = $values+$second[$key]+$third[$key];
}

print_r($result);
?>

方法二:(请避免使用此方法)

$first_result = $first[0]+$second[7]+$third[0];
$second_result = $first[2]+$second[2]+$third[2];
$third_result = $first[1]+$second[1]+$third[1];
于 2013-01-22T05:05:59.890 回答