I’m trying to put values from an array to another array on a specific column.
The context is to see which user was logged at some date.
The Array1 contains the date and the Array2 contains the information about the users.
Array1:
Date
06/01/2022 12:00:00
06/01/2022 12:00:02
06/01/2022 12:00:03
Array2:
IP Users DateHour
192.168.1.1 Snake
192.168.1.3 Mario
192.168.1.4 John Doe
I need to append the DateHour column (Array2) dynamically with the date from the Date column (Array1)
Everytime i’m trying with a for loop or a foreach loop it don’t append correctly and I don’t understand why.
Here is an exemple of what I’ve done:
for ($i=0 ; $i -lt $Array1.Count; $i++){
for ($y=0 ; $y -lt $Array2.Count; $y++){
$Array2.Datehour[$y] += $Array1.Date[$i]
}
}
From my understanding, the Array2.Datehour column will append, but it take long to finish and nothing appear.
Thanks for your help.
>Solution :
Assuming the two arrays are aligned (index N of the first array corresponds to index N in the second array), you only need 1 for loop:
for($i = 0; $i -lt $array1.Count; $i++){
$array2[$i].Datehour = $array1[$i]
}
Note that we index into $array2, not $array2.Datehour – the latter is a "synthetic array" made up of enumerated property values, and assigning to it won’t modify the underlying array item.