如何使用php将数组的一个值与子数组相乘和舍入?

如何使用php将数组的一个值与子数组相乘和舍入?

问题描述:

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!!!

   foreach ($freeTextOutputArrayWithSubArrays as $value) {
        $roundedIndex[] = array($value[0],round($value[1] * 1000));
    }

and result is after var_dump($roundedIndex)

array
  0 => 
    array
      0 => string '

        42631673' (length=18)
      1 => float 559
  1 => 
    array
      0 => string '

        39830245' (length=18)
      1 => float 451
  2 => 
    array
      0 => string '

        9543481' (length=17)
      1 => float 449
  3 => 
    array
      0 => string '

        45349208' (length=18)
      1 => float 448
  4 => 
    array
      0 => string '

        3543584' (length=17)
      1 => float 386

<?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);
foreach ($freeTextOutputArray as $value) {
    $freeTextOutputArrayWithSubArrays[] = explode(":", $value);
}

foreach ($freeTextOutputArrayWithSubArrays as $key => $value) {
    $freeTextOutputArrayWithSubArrays[$key][1] = round($freeTextOutputArrayWithSubArrays[$key][1] * 1000);
}

print_r($freeTextOutputArrayWithSubArrays);