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();
}
Related
CSV:
0021044-1;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021044-2;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021067-1;19/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021087-1;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021087-2;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
I'm splitting the array with the following PHP code:
$csv = array_map(function ($v) {
return str_getcsv($v, ";");
}, file($file);
// Where file stands for the csv file being loaded.
Which gives me the following array
Array
(
[0] => Array
(
[0] => 0021044-1
[1] => 16/02/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[1] => Array
(
[0] => 0021044-2
[1] => 16/02/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[2] => Array
(
[0] => 0021064-1
[1] => 21/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[3] => Array
(
[0] => 0021064-1
[1] => 21/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[4] => Array
(
[0] => 0021067-1
[1] => 19/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[5] => Array
(
[0] => 0021087-1
[1] => 14/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[6] => Array
(
[0] => 0021087-2
[1] => 14/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
)
However, what I want to do is get the lines with the same date and perform a function on them
e.g;
Put the values
0021044-1;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021044-2;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do a function on this array
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do same function on this array
0021067-1;19/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do same function on this array
0021087-1;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021087-2;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do same function on this array
So in short, the function should be applied to a newly generated array out of this one, array as follows:
$new_array = Array
(
[0] => Array
(
[0] => 0021044-1
[1] => 16/02/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[1] => Array
(
[0] => 0021044-2
[1] => 16/02/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
do the function csv2xml($new_array);
And then the second find of the other date etc...
do the function again etc..
All the sub arrays created on their appropriate date will then go to a function which I have covered already in a function called csv2xml($arr)
Though, I'm not succeeding in splitting the array into array's per date.
Can someone guide me in the correct direction?
I think it's a lot of for, while and loops nested in eachother but my brain is currently melting on this..
If the rows will always have the 2 dates following each other you can simply walk through the file calling your function on every other line, with a little check for those dates where only one line exists
I made up a little function to mimic your call that just prints the 2 dates to make sure it works.
function call_function($a1, $a2)
{
echo sprintf( "The 2 dates are %s and %s\n", $a1[1] , $a2[1]);
}
$f = fopen('tst.csv', 'r');
$last_date = NULL;
$last_line = NULL;
while ( ($line = fgetcsv($f, 1024, ';')) !== FALSE){
if ( $line[1] == $last_date ){
// we got the second of a set of dates so call your function
call_function($last_line, $line);
} else {
$last_date = $line[1];
$last_line = $line;
}
}
RESULTS
The 2 dates are 16/02/2022 and 16/02/2022
The 2 dates are 21/01/2022 and 21/01/2022
The 2 dates are 14/01/2022 and 14/01/2022
Hi I have a collection of numbers and I want to find the most occurring number and then the 2nd most occurring and then the third most occurring till 10 and store the result in a array.
The collection looks like this
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => 12
[1] => 194
[2] => 241
[3] => 4
[4] => 29
[5] => 4
[6] => 12
[7] => 15
[8] => 21
[9] => 31
[10] => 281
[11] => 4
[12] => 6
[13] => 4
[14] => 2
[15] => 6
[16] => 4
[17] => 4
[18] => 4
[19] => 15
[20] => 4
[21] => 4
[22] => 13
[23] => 10
[24] => 8
[25] => 8
[26] => 2
[27] => 2
[28] => 2
[29] => 17
[30] => 4
[31] => 20
[32] => 2
[33] => 4
[34] => 20
[35] => 6
)
)
So I want to find the most occurring one and so on till 10th most occurring number.
use array_count_values, here is your reference link http://www.w3schools.com/php/func_array_count_values.asp
<?php
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
?>
Output
Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
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 )
Here's an example in plain english of what I am trying to achieve:
if the number given is 4, then I want to add 1 to every value that is equal to or less than 4 into the corresponding index of another array. (hope that makes sense)
So my first array looks like this:
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 4 [4] => 3 [5] => 2 [6] => 9 [7] => 8 [8] => 1 [9] => 10 [10] => 11 [11] => 12 [12] => 13 [13] => 14 [14] => 15 [15] => 16 [16] => 17 [17] => 18 )
The second array looks like this:
Array ( [0] => 4 [1] => 3 [2] => 4 [3] => 4 [4] => 4 [5] => 5 [6] => 4 [7] => 4 [8] => 5 [9] => 4 [10] => 4 [11] => 5 [12] => 5 [13] => 4 [14] => 4 [15] => 4 [16] => 3 [17] => 3 )
And I am wanting the second array to look like this (after adding 1 to every value below 4 in the first array) so after the addition it would be
Array ( [0] => 4 [1] => 3 [2] => 4 [3] => 5 [4] => 5 [5] => 6 [6] => 4 [7] => 4 [8] => 6 [9] => 4 [10] => 4 [11] => 5 [12] => 5 [13] => 4 [14] => 4 [15] => 4 [16] => 3 [17] => 3 )
In which index 3,4,5,9 have changed.
I think you're looking for array_map
function increase( $m, $n )
{
if( $m < 4 )
{
return $n+1;
}
return $n
}
$arr1;
$arr2;
print_r( array_map("increase", $arr1, $arr2 ) );
Note: this will return a new array.
Using an array_walk
array_walk($arr2,function(&$v,$k) use($arr1) { if($arr1[$k]<=$v){ $v=$v+1;} });
Demo
[or]
A simple foreach will do
foreach($arr1 as $k=>$v)
{
if($v<=$arr2[$k])
{
$arr2[$k]=$arr2[$k]+1;
}
}
print_r($arr2);
Demo
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];