How could I iterate and modify only one value of an array using PHP? I have following code:
<?php
// That's the output that I receive from an Java program.
$freeTextOutput = "
42631673:0.5585319618827993;
39830245:0.4512246398531659;
9543481:0.4486841314390102;
45349208:0.4478453543900579;
3543584:0.3855027425164259;
29266117:0.37859229789438587;
25948743:0.3762908775963289;
44167053:0.3756673145038688;
6355749:0.3702819135046792;
6322651:0.36883803217397765;";
// Transform output into an array.
$freeTextOutputArray = explode(";", $freeTextOutput, -1);
Than I have following array:
Array (
[0] => 42631673:0.5585319618827993
[1] => 39830245:0.4512246398531659
[2] => 9543481:0.4486841314390102
[3] => 45349208:0.4478453543900579
[4] => 3543584:0.3855027425164259
[5] => 29266117:0.37859229789438587
[6] => 25948743:0.3762908775963289
[7] => 44167053:0.3756673145038688
[8] => 6355749:0.3702819135046792
[9] => 6322651:0.36883803217397765
)
Than I explode it, so I have an array with subarrays:
// Transform values of the first array into subarrays.
foreach ($freeTextOutputArray as $value) {
$freeTextOutputArrayWithSubArrays[] = explode(":", $value);
}
The result is:
Array (
[0] => Array (
[0] => 42631673 [1] => 0.5585319618827993
)
[1] => Array (
[0] => 39830245 [1] => 0.4512246398531659
)
etc...
[9] => Array (
[0] => 6322651 [1] => 0.36883803217397765
)
)
Now I would like to multiply the value of key[1] of the subarrays by 1000 and round it. I tried just like that:
foreach ($freeTextOutputArrayWithSubArrays as $value) {
foreach ($value as $wert) {
$roundedIndex[] = round($wert * 1000);
}
}
But then, everything was multiplied and the subarrays are gone:
Array (
[0] => 42631673000
[1] => 559
[2] => 39830245000
[3] => 451
etc...
[18] => 6322651000
[19] => 369
)
So what I have to do to have the following result in the end?
Array (
[0] => Array (
[0] => 42631673 [1] => 558
)
[1] => Array (
[0] => 39830245 [1] => 451
)
etc...
[9] => Array (
[0] => 6322651 [1] => 368
)
)
Thank you in advance!!!