dongzhan9100 2014-08-17 15:34
浏览 22
已采纳

如何使用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!!!

  • 写回答

2条回答 默认 最新

  • dongliyan7318 2014-08-17 15:43
    关注
       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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化