PHP value=arraykey + arraykey + arraykey [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
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:
X = 458752 => i=0, j=7, k=0 which is 458752 = 0 + 458752 + 0
X = 131586 => i=2, j=2, k=2 which is 131586 = 256 + 131072 + 2
X = 65793 => i=1, j=1, k=1 which is 65793 = 256 + 65536 + 1

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

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.

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];

Related

Array to column row with SQL Update column in MySQL table

I have array named olmali
$olmali = $_POST['result'];
And
print_r($olmali);
Result is below:
Array (
[0] => 1
[1] => 1
[2] => 20
[3] => 2
[4] => 3
[5] => 5
[6] => 6
[7] => 7
[8] => 9
[9] => 8
[10] => 10
[11] => 11
[12] => 13
[13] => 12
[14] => 12
[15] => 14
[16] => 15
[17] => 16
[18] => 17
[19] => 17
[20] => 19
[21] => 20
)
I want to use SQL UPDATE command and I expect:
id test
1 1
2 1
3 20
4 2
5 3
6 ....and goes on
How can I resolve this problem? Is there any way and how can I do it. PHP array to column row in MySQL table with UPDATE SQL command like that
I would suggest to start from the end with a for loop and reassign every key with a value +1. Then, you just have to remove the first index of the array with unset(). See the code below.
$olmali = [
0 => 1,
1 => 1,
2 => 20,
3 => 2,
4 => 3,
5 => 5,
6 => 6,
7 => 7,
8 => 9,
9 => 8,
10 => 10,
11 => 11,
12 => 13,
13 => 12,
14 => 12,
15 => 14,
16 => 15,
17 => 16,
18 => 17,
19 => 17,
20 => 19,
21 => 20
];
for($i = count($olmali); $i > 0; $i--) {
$olmali[$i] = $olmali[$i - 1];
}
unset($olmali[0]);
print_r($olmali);
Output:
Array ( [1] => 1 [2] => 1 [3] => 20 [4] => 2 [5] => 3 [6] => 5 [7] => 6 [8] => 7 [9] => 9 [10] => 8 [11] => 10 [12] => 11 [13] => 13 [14] => 12 [15] => 12 [16] => 14 [17] => 15 [18] => 16 [19] => 17 [20] => 17 [21] => 19 [22] => 20 )
Assuming
$myArray = array (
[0] => 1
[1] => 1
[2] => 20
[3] => 2
[4] => 3
[5] => 5
[6] => 6
[7] => 7
[8] => 9
[9] => 8
[10] => 10
[11] => 11
[12] => 13
[13] => 12
[14] => 12
[15] => 14
[16] => 15
[17] => 16
[18] => 17
[19] => 17
[20] => 19
[21] => 20
);
then once you have a db connection using a PDO prepare binding and execute
$stmt = $conn->prepare("Update my_tabe
set my_column = :my_value
where my_id_comn = :my_id +1" );
$stmt->bindParam(':my_value', $value);
$stmt->bindParam(':my_id', $key);
foreach ($myArray as $key => $value){
$stmt->execute();
}

Selecting only even numbers in php output [duplicate]

This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 6 years ago.
I am trying to select and display only the even number in a separate output from this below
function toms($c,$first = 0,$second = 1)
{
$toms = [$first,$second];
for($i=1;$i<$c;$i++)
{
$toms[] = $toms[$i]+$toms[$i-1];
}
return $toms;
}
echo "<pre>";
print_r(toms(33));
?>
currently this outputs
array
(
[0] => 0
[1] => 1
[2] => 1
[3] => 2
[4] => 3
[5] => 5
[6] => 8
[7] => 13
[8] => 21
[9] => 34
[10] => 55
[11] => 89
[12] => 144
[13] => 233
[14] => 377
[15] => 610
[16] => 987
[17] => 1597
[18] => 2584
[19] => 4181
[20] => 6765
[21] => 10946
[22] => 17711
[23] => 28657
[24] => 46368
[25] => 75025
[26] => 121393
[27] => 196418
[28] => 317811
[29] => 514229
[30] => 832040
[31] => 1346269
[32] => 2178309
[33] => 3524578
)
Anyone know how I can display only the even numbers returned, so I would want to have 2, 8, 34 and so on
thank you
You can use array_filter
print_r(array_filter(toms(33), function($number){
return $number % 2 == 0;
}));
Or if you want to filter out 0:
print_r(array_filter(toms(33), function($number){
return $number != 0 && $number % 2 == 0;
}));
A bit more readable:
$isEvenNumber = function($number) {
return $number % 2 == 0;
}
$numbers = toms(33);
$filtered_numbers = array_filter($numbers, $isEvenNumber);
var_dump($filtered_numbers);

How to sum array value in PHP?

I have 2 array variable I want to sum array and divide into two parts. Please look my code -
print_r($public);
output -
Array
(
[0] => 2
[1] => 3
[2] => 5
[3] => 2
[4] => 1
[5] => 32
[6] => 5
[7] => 20
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
[13] => 0
[14] => 7
[15] => 0
[16] => 0
[17] => 0
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 0
[23] => 0
[24] => 0
[25] => 0
[26] => 11
[27] => 0
[28] => 0
[29] => 0
[30] => 0
)
print_r($private);
Array
(
[0] => 0
[1] => 1
[2] => 0
[3] => 0
[4] => 1
[5] => 0
[6] => 0
[7] => 3
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
[13] => 0
[14] => 7
[15] => 0
[16] => 0
[17] => 0
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 4
[23] => 0
[24] => 0
[25] => 0
[26] => 0
[27] => 0
[28] => 2
[29] => 0
[30] => 0
)
My Output should be -
$variable1=array_sum($public) + array_sum($private); //for First 15 days(array)
$variable2 = array_sum($public) + array_sum($private); //For 16 to end of the array
$public and $private is two array. contans May month date wise records. I want to sum array value into two divide parts.
1st - Day - 1-15(sum of array 0 to 14)
2nd - Day - 16-end of month(sum of array 15 to end of the array)
How to calculate both variable into two divide parts in a single line code?
You can use array_slice:
$first_part = array_sum(array_slice($public, 0, 15));
$second_part = array_sum(array_slice($public, 15, 16));
$first_part = array_sum(array_slice($public, 0, 15)) + array_sum(array_slice($private, 0, 15));
$second_part = array_sum(array_slice($public, 15, 16)) + array_sum(array_slice($private, 15, 16));

How to reverse an array [duplicate]

This question already has answers here:
Reverse array in php
(6 answers)
Closed 8 years ago.
So this is my array, I have tried rsort and array_reverse but its sorting it numerically and that's not what I want. I just want it in the opposite order.
[15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16]
You need to add true to your array_reverse:
array array_reverse ( array $array [, bool $preserve_keys = false ] )
In other words, try something like this:
array_reverse( $yourArray, true);
This way your keys are preserved and you achieve what you need.
array_reverse should work. I just did this test:
$array = array(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16);
$rArray = array_reverse($array);
print_r($array);
print_r($rArray);
and the results were:
//for $array
Array ( [0] => 15 [1] => 14 [2] => 13 [3] => 12 [4] => 11 [5] => 10 [6] => 9 [7] => 8 [8] => 7 [9] => 6 [10] => 5 [11] => 4 [12] => 3 [13] => 2 [14] => 1 [15] => 30 [16] => 29 [17] => 28 [18] => 27 [19] => 26 [20] => 25 [21] => 24 [22] => 23 [23] => 22 [24] => 21 [25] => 20 [26] => 19 [27] => 18 [28] => 17 [29] => 16 )
//for $rArray
Array ( [0] => 16 [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => 21 [6] => 22 [7] => 23 [8] => 24 [9] => 25 [10] => 26 [11] => 27 [12] => 28 [13] => 29 [14] => 30 [15] => 1 [16] => 2 [17] => 3 [18] => 4 [19] => 5 [20] => 6 [21] => 7 [22] => 8 [23] => 9 [24] => 10 [25] => 11 [26] => 12 [27] => 13 [28] => 14 [29] => 15 )
In order to reverse an array simply put it in a stack (or at least use that logic).
FILO (First in last out) thus you have swapped the order.
A Stack Overflow question around php stacks can be found here:
PHP Stack Implementation
Are you familiar with other languages? I don't use php much.
Simply create a new object and add the end to it. Ex
new Array a
a.push(oldArray.removelast())...
The do-it-yourself-and-more-fun solution:
$oldArray = array(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16);
$length = count($oldArray);
$start= $length - 1;
$newArray=array();
for ($x = $start; $x >=0; $x--)
{
array_push($newArray,$oldArray[$x]);
}
print_r($newArray);
Prints:
Array ( [0] => 16 [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => 21 [6] => 22 [7] => 23 [8] => 24 [9] => 25 [10] => 26 [11] => 27 [12] => 28 [13] => 29 [14] => 30 [15] => 1 [16] => 2 [17] => 3 [18] => 4 [19] => 5 [20] => 6 [21] => 7 [22] => 8 [23] => 9 [24] => 10 [25] => 11 [26] => 12 [27] => 13 [28] => 14 [29] => 15 )

Loop logic goes wrong while adjusting array

I have an array that represents UNIX time in int type from a table I can't change. Some of the rows are not full UNIX timestamps but are short by a couple of integers. There is a reason this is so in the table but for my script, I need the string to change the non 10 digit rows into "0" and the 10 digit ones into date("Ymd",?) form. Here's an example of the array $qucls:
Array
(
[0] => 1332594303
[1] => 1330960502
[2] => 1331227649
[3] => 1331305503
[4] => 1332594303
[5] => 1331147102
[6] => 1332680702
[7] => 1331301902
[8] => 1331048163
[9] => 1332248704
[10] => 1332421503
[11] => 31536000
[12] => 1331816703
[13] => 604800
[14] => 0
[15] => 31536000
[16] => 1332248703
[17] => 31536000
[18] => 1361922903
)
This is the script:
$k=0
$l=0
foreach ($qucls as $dt[$k]){
if (strlen($dt[$k]) < 10)
$dt[$k++] = '0';
else {$dt[$k++] = date("Ymd", $dt[$l++]);
}
}
for ($l=0; $l < $k; $l++){
}
This is the outcome after the loop:
Array
(
[0] => 20120324
[1] => 20120305
[2] => 20120308
[3] => 20120309
[4] => 20120324
[5] => 20120307
[6] => 20120325
[7] => 20120309
[8] => 20120306
[9] => 20120320
[10] => 20120322
[11] => 0
[12] => 19700101
[13] => 0
[14] => 0
[15] => 0
[16] => 19700817
[17] => 0
[18] => 19700101
)
Notice the date form is formatted properly until it reaches the 1st integer that is strlen < 10. At that point, it changes the less than 10 length integer to "0" which is correct, but the dates after that are goofed up. It continues to change the < 10 digit ones to 0 correctly.
Can someone help me figure out what is wrong in this loop? I'm not quite getting the right outcome with all those 1970 dates after the ELSE kicks in. I'm still new at this.
Thank you.
Use the below script
<?php
$qucls = array(
0 => 1332594303,
1 => 1330960502,
2 => 1331227649,
3 => 1331305503,
4 => 1332594303,
5 => 1331147102,
6=> 1332680702,
7=> 1331301902,
8=> 1331048163,
9=> 1332248704,
10 => 1332421503,
11 => 31536000,
12 => 1331816703,
13 => 604800,
14 => 0,
15 => 31536000,
16 => 1332248703,
17 => 31536000,
18 => 1361922903
);
foreach ($qucls as $key=>$value){
if (strlen($value)< 10){
$dt[] = 0;
}else{
$dt[] = date("Ymd", $value);
}
}
echo "<pre>";
print_r($array);
print_r($dt);
exit;
?>
and you will get the below output
Array
(
[0] => 20120324
[1] => 20120305
[2] => 20120308
[3] => 20120309
[4] => 20120324
[5] => 20120307
[6] => 20120325
[7] => 20120309
[8] => 20120306
[9] => 20120320
[10] => 20120322
[11] => 0
[12] => 20120315
[13] => 0
[14] => 0
[15] => 0
[16] => 20120320
[17] => 0
[18] => 20130226
)

Categories