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 3 years ago.
Improve this question
In my foreach loop, I'm fetching data from csv file, after rendering it in the array, the data is rendered collectively, but I want it to show column wise as in csv file or I have shown in the below second code example:
Array(
[0] => op1
[1] => op2
[2] => ans 1
[3] => op4
[4] => op1
[5] => op2
[6] => ans 2
[7] => op4
[8] => op1
[9] => op2
[10] => ans 3
[11] => op4
[12] => op1
[13] => op2
[14] => ans 4
[15] => op4
[16] => op1
[17] => op2
[18] => ans 5
[19] => op4
[20] => op1
)
I want to this
Array
(
option[0]=>
Array(
[0] => op1
[1] => op2
[2] => ans 1
[3] => op4
)
option[1]=>
Array(
[0] => op1
[1] => op2
[2] => ans 2
[3] => op4
)
option[2]=>
Array(
[0] => op1
[1] => op2
[2] => ans 2
[3] => op4
)
option[3]=>
Array(
[0] => op1
[1] => op2
[2] => ans 2
[3] => op4
)
)
you can use array_chuck() function like this:
print_r(array_chunk($input_array, 4));
In your existing foreach loop use Modulus for divide array in groups.
$final_arr= array();
$new_key = 0;
foreach($csv_data as $key => $value){
$new_key = $new_key;
if($key % 4 == 0){
$new_key += 1;
}
$final_arr[$new_key][]= $value;
}
Here I assume you got your csv data in $csv_data this variable.
Hope it helps to you.
From str_getcsv's manual page, first two comments:
That does not produce the desired results, if fields contain
linebreaks.
Handy one liner to parse a CSV file into an array:
$csv = array_map('str_getcsv', file('data.csv'));
This will create an array of associative arrays with the first row
column headers as the keys.
$csv = array_map('str_getcsv', file($file));
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv); // remove column header
Related
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 2 years ago.
Improve this question
I have an array with many dates. These dates are passive. If the free time in between is more than 8 days, i need to get the opposite of these dates. I have to process these opposite dates as the number of day, start date and end date.
Example Array
Array
(
[0] => 17-05-2020
[1] => 18-05-2020
[2] => 19-05-2020
[3] => 20-05-2020
[4] => 28-05-2020
[5] => 29-05-2020
[6] => 30-05-2020
[7] => 02-06-2020
[8] => 03-06-2020
[9] => 10-07-2020
[10] => 10-07-2020
[11] => 11-07-2020
[12] => 12-07-2020
[13] => 13-07-2020
[14] => 20-07-2020
[15] => 21-07-2020
[16] => 25-07-2020
[17] => 26-07-2020
[18] => 27-07-2020
)
The result I want
Array
(
[0] => Array
(
[0] => 30-05-2020
[1] => 02-06-2020
[2] => 3
)
[1] => Array
(
[0] => 13-07-2020
[1] => 20-07-2020
[2] => 7
)
[2] => Array
(
[0] => 21-07-2020
[1] => 25-07-2020
[2] => 4
)
)
What i understood is you want to get difference between dates and if difference is >= 8 then you want those dates in separate array with days counts as well
Do like below:
$finalArray = [];
foreach($array as $key=>$arr){
if(isset($array[$key+1])){
$diff = round((strtotime($array[$key+1]) - strtotime($arr)) / (60 * 60 * 24));
if(($diff < 8) and ($diff > 1)){
$finalArray[] = array(
$arr,
$array[$key+1],
$diff
);
}
}
}
print_r($finalArray);
Output : https://3v4l.org/QBYYR
I have two arrays like this:
$array_1 = Array ( [0] => 4 [1] => 6 [2] => 2 [3] => 6 [4] => 4 [5] => 10 [6] => 4 [7] => 6 [8] => 2 [9] => 2 [10] => 4 [11] => 4 [12] => 2 [13] => 2 );
$array_2 = Array ( [0] => DK [1] => GA [2] => DK [3] => GA [4] => DK [5] => GA [6] => WE [7] => VE [8] => WE [9] => VE [10] => PLA [11] => PRA [12] => PLA [13] => PRA ) ;
Now I want result like this:
$dk=4+2+4=10;
$ga=6+6+10=22;
$we=4+2=6;
$ve=6+2=8;
$pla=4+2=6;
$pra=4+2;
Explanation:
In $array_2, 'DK' exists 3 times and key values are = 0,2 and 4.
So, i have to add the values of $array_1 having key 0,2,4 and assign them to $dk. Here, $dk will be 4+2+4=10. This process will be same for all other variables.
How can i do this??
Instead separate variable name I suggest you to make array like this
<?php
$array_1 = [4,6,2,6];
$array_2 = [ 0=> "DK", 1=>"GA", 2=>"DK", 3=>"GA"];
$newArray = [];
foreach($array_2 as $key=>$value){
if(isset($newArray[$value])){
$newArray[$value] +=$array_1[$key];
}else{
$newArray[$value] =$array_1[$key];
}
}
print_r($newArray);
?>
Live Demo
Output :
Array
(
[DK] => 6
[GA] => 12
)
Another suggestion : Instead complex programming try to make good relation or binding to not get any inconsistency in records
This will loop array2 and build an array with the sum.
Then output it (just to see the result), then I use extract to pull out the variables as you want them.
But I would rather keep them in the array
Foreach($array_2 as $key => $val){
If(!isset($new[$val])) $new[$val] =0;
$new[$val] += $array_1[$key];
}
Var_dump($new);
Extract($new);
https://3v4l.org/jOR7Z
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to split the below array into 3 single arrays. However, the size of the array can be any number. The below array size is 3.
Array (
[0] => Array
(
[0] => a
[1] => bb
[2] => c
[3] => dd
[4] => ee
)
[1] => Array
(
[0] => dd
[1] => ff
[2] => hh
[3] => iji
[4] => kkk
[5] => a
[6] => cc
)
[2] => Array
(
[0] => ee
[1] => kk
[2] => iji
[3] => a
[4] => bb
[5] => lmn
[6] => ppq
[7] => xyz
)
)
Expected output:
Array1 (
[0] => a
[1] => bb
[2] => c
[3] => dd
[4] => ee
)
Array2 (
[0] => dd
[1] => ff
[2] => hh
[3] => iji
[4] => kkk
[5] => a
[6] => cc
)
Array3 (
[0] => ee
[1] => kk
[2] => iji
[3] => a
[4] => bb
[5] => lmn
[6] => ppq
[7] => xyz
)
The name of the individual array should be followed by a number that increments for each array.
You can break a 3d array into 3 separate arrays using list().
list($array1, $array2, $array3) = $mainArray;
Here's an example: https://3v4l.org/hQ1Te
Now if you don't know how many arrays are going to be in the input array you can do something like this...
for($i=0; $i<count($mainArray); $i++){
$variableName = "array$i";
$$variableName = $mainArray[$i];
}
var_dump($array1, $array2, $array3, $array4, ...);
So are you looking for something like this?
for ($i = 0; $i < count($inputArray); $i++) {
echo "Array ". $i;
echo "<pre>";
print_r($inputArray[$i]);
echo "</pre>";
}
Edit: ok now I get it, you want:
for ($i = 0; $i < count($inputArray); $i++) {
$name = "array" . ($i + 1);
$$name = $inputArray[$i];
}
$blueswards array:
Array ( [0] => 3 [1] => 8 [2] => 1 [3] => 4 [4] => 9 )
$redswards array:
Array ( [0] => 2 [1] => 9 [2] => 3 [3] => 6 [4] => 9 )
What i try to do:
Array ( [0] => 3 [1] => 8 [2] => 1 [3] => 4 [4] => 9 [5] => 2 [6] => 9 [7] => 3 [8] => 6 [9] => 9 )
I cant do it with array_merge.
EDIT = Sorry everyone i did it with array merge now its working. :[
I used array_merge() using your array values. And, it worked.
<?php
$new_array[] = array();
$blueswards = array(3,8,4,9);
$redswards = array(2,9,3,6,9);
$new_array = array_merge($blueswards,$redswards);
print_r($new_array);
?>
OUTPUT:
Array ( [0] => 3 [1] => 8 [2] => 4 [3] => 9 [4] => 2 [5] => 9 [6] => 3 [7] => 6 [8] => 9 )
Why don't you just run them in a loop once they're populated.
$newarray=0;
$arrayCounter=0;
for (i=0; i<count($redswards)-1 ; i++){
$newarray[$arrayCounter] = $redswards[i];
$arrayCounter++
}
for (i=0; i<count($blueswards)-1 ; i++){
$newarray[$arrayCounter] = $blueswards[i];
$arrayCounter++
}
There... That will do it correctly by simply adding on to the new array. It will also count the number of times it has to loop so you don't have to hardcode that.
I mean, that's really simple, and you may have to change the hardcoded i values. But that's the basic idea.
Used durbnpolsns code and edited it to work.
$newarray=0;
for ($i=0; $i<4 ; $i++){
$newarray[$i] = $redswards[$i];
}
$j=0;
for ($i=5; $i<8 ; $i++){
$newarray[$i] = $blueswards[$j];
$j++;
}
I have not tested the code and formatting may be impossible, but I'm typing on my phone.
Sorry for that.
very basic question however I have had some trouble finding the answers on PHP.NET.
I have the following array:
Array (
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
)
I want the array to be re-ordered so that the key number 3 in the first series of the array becomes the first, then the rest to be re-ordered from there to eventually get the result of:
Array (
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
)
I am looking for a way to do this so I can define the array, then the first level key I need to sort by, and then it will return the array in this way.
The standard PHP keys didn't seem to offer something like this, so it would be good to be able to have a separate function such as $newArray = reorder_array($array, $key);
I don't require any sorting of the second level, only the initial 4 main / first level array sections.
You help is greatly appreciated as I have been sitting on this one for awhile without a clear and simple solution.
You re-ordering can be simply implemented with one foreach loop, like:
function reorderArray($array, $key)
{
$found = false;
foreach($array as $k=>$v)
{
$found = $found || $k===$key;
if(!$found)
{
unset($array[$k]);
$array[$k] = $v;
}
//else break can be added for performance issues
}
return $array;
}
with usage
$array=[1=>'foo', 4=>'bar', 9=>'baz', 'test'=>51];
var_dump(reorderArray($array, 9));
var_dump(reorderArray($array, 'test'));
var_dump(reorderArray($array, 'no_such_key'));//original array in result
-check this demo. If keys are consecutive numerics, however, this can be easily implemented with array_slice() calls.