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();
}
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);
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));
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 )
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
)