convert the array to the correct form - php

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

Related

Convert csv into array but split array by same date and execute a function on each array

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

Array merge failed

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

How to get values in an array with many arrays in php?

I am confused with this list of array. I have a text file that contains the values that I will need to insert to the database. I have exploded it so that I can get those separated values.
REP 1020001,3,140822,140822;0111,260.00,23,34,3,54,1,2,4,5,12,23,46;0214,22.00,32,4,11,25,4,12,23,5,2,2,44;0313,25.00,5,52,12,45,12,5,6,7,12,3,33;
My code is just like this:
$read = FileRead($tmp);
$perline = explode(";",$read);
for($j=0; $j<count($perline); $j++)
{
$percomma = explode(",", $perline[$j]);
print_r ($percomma);
}
And the result is this:
Array ( [0] => 1020001 [1] => 3 [2] => 140822 [3] => 140822 ) Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) Array ( [0] => )
What should I do to get the value from the 2nd array to the 4th array? Should I put it inside another array to make it multidimensional? Or there are other ways to solve this?
EDIT
My question is how will I be able to get the values from the print_r($percomma) when there are a lot of arrays in the result. The result array is up there that says And the result is this
EDIT 2
As making the array to be multidimensional I get this as a result:
Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) ) Array ( [0] => Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) ) Array ( [0] => Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) ) Array ( [0] => Array ( [0] => ) )
It shows that they're all individual arrays. My code is this:
$read = FileRead($tmp);
$perline = explode(";",$read);
for($j=0; $j<count($perline); $j++)
{
$percomma = explode(",", $perline[$j]);
$entries = array($percomma);
print_r ($entries);
}
EDIT 3
From subas_poudel's answer I get this result:
Array
(
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
[2] => Array
(
[0] => 0313
[1] => 25.00
[2] => 5
[3] => 52
[4] => 12
[5] => 45
[6] => 12
[7] => 5
[8] => 6
[9] => 7
[10] => 12
[11] => 3
[12] => 33
)
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
[2] => Array
(
[0] => 0313
[1] => 25.00
[2] => 5
[3] => 52
[4] => 12
[5] => 45
[6] => 12
[7] => 5
[8] => 6
[9] => 7
[10] => 12
[11] => 3
[12] => 33
)
)
The last set of array is what I needed but how can I get those values?
EDIT 4
With my simple mistake I put the array_slice inside my for loop that's why I get too many arrays. Now this is the result of #subas_poudel's answer.
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
[2] => Array
(
[0] => 0313
[1] => 25.00
[2] => 5
[3] => 52
[4] => 12
[5] => 45
[6] => 12
[7] => 5
[8] => 6
[9] => 7
[10] => 12
[11] => 3
[12] => 33
)
)
you can use multidimensional array to hold all the value. then you can get all the array you want by either $percomma[index you want] or using array_slice.
$read = FileRead($tmp);
$perline = explode(";",$read);
for($j=0; $j<count($perline); $j++)
{
$percomma[] = explode(",", $perline[$j]);
}
echo '<pre>'.print_r (array_slice($percomma,1,3),true).'</pre>';
$array = array
(
array("bla",22,18),
array("blaa",15,13),
array("blaaa",5,2),
array("blaaaa",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$array[$row][$col]."</li>";
}
echo "</ul>";
}
I'm going to copy/edit your original code posting, to show what I was talking about in my comment.
$read = FileRead($tmp);
$perline = explode(";",$read);
$percomma = array(); //new array-declaration line
for($j=0; $j<count($perline); $j++)
{ $percomma[j] = explode(",", $perline[$j]);
print_r ($percomma[j]);
}
Your first "And the result is" line is a printed sequence of arrays, and would be unchanged by implementing the above tweaks to your code. However, because the values in the $percomma variable are no longer overwritten in each loop that obtains an array from the explode() function, you may now, at any point after the above code, do something like:
print_r($percomma[1]); //re-print 2nd array, or
$ary_el = $percomma[2][1]; //get 2nd element of 3rd array (22.00) into a variable.

Changing values in array based on values in another array

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

Set array keys to value of another arrays values - PHP

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.

Categories