-8

I Have 3 Arrays

Array 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 )

Array 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 )

Array 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 )

Given a number X which I know was calculated as a1[i] + a2[j] + a3[k] how can I calculate i, j and k?

Examples:

  1. X = 458752 => i=0, j=7, k=0 which is 458752 = 0 + 458752 + 0
  2. X = 131586 => i=2, j=2, k=2 which is 131586 = 256 + 131072 + 2
  3. X = 65793 => i=1, j=1, k=1 which is 65793 = 256 + 65536 + 1
5
  • 1
    Question i s not clear can you explain it more. Commented Jan 22, 2013 at 4:58
  • The answer is "with code". There is no function or accepted way of doing this. You just need to program a solution, which is too localized for Stack Overflow. Hint: The answer will involved the [], or "array indexing" operator. You should sit down and read a tutorial on PHP. Commented Jan 22, 2013 at 5:01
  • PrasanthBendra I want to fill the missing sum to getting $value $value=?+?+? that the point meagar That the reason I asked here because I cannot find any match function to doing this. 458752 is the defined/ inputed value $value the 0+458752+0 is array how to calculated it, look like fill a missing value till it equal but with an array key Commented Jan 22, 2013 at 5:19
  • I think I know what you want. I rephrased your question (and also adjusted your examples to 0 based indexes). Revert my edit if I got it wrong. Commented Jan 22, 2013 at 8:20
  • Thank You, that is my goal, sorry for my bad english Commented Jan 22, 2013 at 8:53

3 Answers 3

0

How about:

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";
}

output:

x=0, i=0, j=7, k=0
x=514, i=2, j=2, k=2
x=257, i=1, j=1, k=1
1
  • Hahaha Solved! This is what I want Thank You sir! :D I managed to running it via loop before take a age to load, but your code run as wind Thank You!! Commented Jan 22, 2013 at 12:54
0

This

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

should work.

Array indexing begins from 0, so $array[1] is the technically the second element in an array.

1
  • No it not I mean I want to get equal value bassicaly If array1>$value OR value $array1=$value then loop stop but if $array1 not yet equal then it will be + in array2 till equal and so on Commented Jan 22, 2013 at 5:11
-1

First you try to post your questions clearly.

Your first array count is mismatched with other two arrays. So if the 3 arrays count must be equal, easily you can get the values. if you need to customize addition value, hard coded the array values and try addition.

Method1: All the 3 arrays count is same.

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

Method 2: (Please avoid this method)

$first_result = $first[0]+$second[7]+$third[0];
$second_result = $first[2]+$second[2]+$third[2];
$third_result = $first[1]+$second[1]+$third[1];
3
  • Hei I Love Your code It almost what I want, can I revese the value? I defined the $value result and get $first[0]+$second[7]+$third[0]; That what I mean 458752=$first[0]+$second[7]+$third[0]; Thank You Very Much but the problem not yet solved Commented Jan 22, 2013 at 5:26
  • If you want hard coded result is bad thing to code, its not possible to develop dynamically. Commented Jan 22, 2013 at 5:34
  • It quite difficult to say the question, because it's revese the result to what value was sum it, I entered the just set $first_result and get =$first[0]+$second[7]+$third[0]; just that the point. Commented Jan 22, 2013 at 5:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.