Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Put value from an array to another array specific column

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading