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
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
After converting the file
$rows = array_map('trim', file($lines1));
foreach ($rows as $key => $value) {
$params = array_map('trim', explode(';', $value));
}
an array of the following type is obtained, which is contained in a variable:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => i need this [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 )
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => i need this [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 )
etc...
how can you combine these arrays and put the values with the key = 3 into separate variables.
View:
Array ( [0] => i need this [1] => i need this and etc...
$range = range(1, 100);
$result = array_combine($range, $range);
check array_combine
I have this array, there are 4 values = 3, how can i delete just two of them?
Array (
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
[5] => 3
[6] => 3
[7] => 2
[8] => 2
[9] => 2
[10] => 1
[11] => 2
[12] => 3
[13] => 2
)
I already tried unset(). is there any way to achieve this ?
so the array looks like this
Array (
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
[5] => 3
[6] => 2
[7] => 2
[8] => 2
[9] => 1
[10] => 2
[11] => 2
)
Just make two calls to unset, using array_search:
$array = array(1,2,3,3,3,4,5,6);
print_r($array);
unset($array[array_search(3, $array)]);
unset($array[array_search(3, $array)]);
print_r($array);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 3
[4] => 3
[5] => 4
[6] => 5
[7] => 6
)
Array
(
[0] => 1
[1] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
)
But this assumes that you are OK with the behavior of array_search, which would return the first index which matches the value 3. If you had some other order of removal in mind, then state it, and the code might have to be changed.
You might use a loop and skip the first 2 times you encounter a 3. Then for the next matches, you could use unset. Then you might use array_values to reset the keys:
$items = [
1,2,1,2,3,3,3,2,2,2,1,2,3,2
];
$found = 0;
foreach ($items as $key => $item) {
if ($item === 3) {
if ($found < 2) {
$found++;
continue;
}
unset($items[$key]);
}
}
print_r(array_values($items));
Result
Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
[5] => 3
[6] => 2
[7] => 2
[8] => 2
[9] => 1
[10] => 2
[11] => 2
)
Php demo
maybe it's simple as it look, but it driving me crazy.
Here's my array :
$result =
Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 4
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
[9] => 4
[10] => 40
)
[1] => Array
(
[0] => 5
[1] => 4
[2] => 4
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
[9] => 4
[10] => 41
)
[2] => Array
(
[0] => 5
[1] => 5
[2] => 5
[3] => 5
[4] => 5
[5] => 5
[6] => 5
[7] => 5
[8] => 5
[9] => 5
[10] => 50
)
)
this is what i want transformed into :
after array_merge:
$result =
Array
(
[0] => 4
[1] => 4
[2] => 4
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
[9] => 4
[10] => 40
[11] => 5
[12] => 4
[13] => 4
[14] => 4
[15] => 4
[16] => 4
[17] => 4
[18] => 4
[19] => 4
[20] => 4
[21] => 41
[22] => 5
[23] => 5
[24] => 5
[25] => 5
[26] => 5
[27] => 5
[28] => 5
[29] => 5
[30] => 5
[31] => 5
[32] => 50
)
this is the code :
<?php
$result = array();
?>
#foreach ($detail_ratings as $detail_rating)
<?php $result[] = json_decode($detail_rating); ?>
#endforeach
<?php
$result = array_merge($result[0],$result[1],$result[2]);
?>
{{print_r($result)}}
how do i make it automatically without manually using this code :
$result = array_merge($result[0],$result[1],$result[2]);
this is what i already did :
foreach ($result as $key => $value) {
$values[] = array_merge($value,$result[$key]);
}
Use this
$result=array();
foreach($values as $value) {
$result = array_merge($result,$value);
}
Maybe this can help :-
function nameit(array $arr)
{
$newarr = array();
foreach($arr as $a => $b)
{
$newarr[] = $b;
}
return $newarr;
}
Try this:
$final_array = array_merge($array1,$array2);
I have the following:
( [0] => 3 [1] => 2 [2] => 12 [3] => 6 [4] => 8 [5] => 7 [6] => 9 [7] => 10 [8] => 5 [9] => 4 )
I want to use the value of each of those as the key in:
( [0] => 7 [1] => 2 [2] => 10 [3] => 3 [4] => 5 [5] => 6 [6] => 11 [7] => 9 [8] => 4 [9] => 8 )
I've tried this:
foreach ($iOrder as $i)
{
$pOrder[$i] = $pOrder[$p];
$p++;
}
I get this:
( [12] => 2 [10] => 6 [5] => 5 [4] => 7 )
Any thoughts?
Do you mean
$result = array_combine($keys, $values);
?
array_combine()
Frist array, $arr1,
( [0] => 3 [1] => 2 [2] => 12 [3] => 6 [4] => 8 [5] => 7 [6] => 9 [7] => 10 [8] => 5 [9] => 4 )
Second array, $arr2,
( [0] => 7 [1] => 2 [2] => 10 [3] => 3 [4] => 5 [5] => 6 [6] => 11 [7] => 9 [8] => 4 [9] => 8 )
If I understood correct, you want to use the values of $arr1 as keys in $arr2.
$values = array_values($arr1);
=> This gives you the values of $arr1.
you can use, array_combine($keys, $values);
so the resulting array would be,
$result_arr = array_combine(array_values($arr1), array_values($arr2));
However, it may not work as expected if the no of items in two arrays are different.