-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]ように計算することができますか?ijk

例:

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

3 に答える 3

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 に答える
0

これ

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

動作するはずです。

配列のインデックスは 0 から始まるため$array[1]、技術的には配列の 2 番目の要素です。

于 2013-01-22T04:57:37.450 に答える
-1

まず、質問を明確に投稿してみてください。

最初の配列数が他の 2 つの配列と一致しません。したがって、3 つの配列の数が等しくなければならない場合は、簡単に値を取得できます。加算値をカスタマイズする必要がある場合は、配列値をハードコーディングして加算を試してください。

Method1: 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);
?>

方法 2: (この方法は避けてください)

$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 に答える