How to multiply and round only one value of an array with subarrays using php?

Aloysia de Argenteuil

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

Muhammad Ali
   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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

using array_unique in php codeigniter the returing only one value

How to get only one value in Javascript array of objects using for of and for in statements?

How to synchronously nest subarrays from one array into subarrays of another?

How to efficiently round only one column of numpy array to nearest 0.5?

Using PHP to "multiply" array values

PHP: If child array has one value only, replace the array with that value

how to get only one value from an array?

How to multiply matrices with only one iterable

Check if value is the only thing in an array using php

PHP array_filter to get only one value from an array

php filter array of array to only one property value

How to create an array with associative subarrays from a one-dimensional array?

remove subarrays in an array php

how to multiply array in multi dimensional array in php

How to delete one value from array php

how to split a numpy array into subarrays based on values of one colums

Angular $HTTP Post method only getting one array value in PHP

How can I make an ATM machine with PHP by using only one loop and an array?

How to output only one value of an array within an array of a state in React?

PHP: how to compare value of one array with value to other array in the loop

How to multiply each value in an array of coordinates

How to group the array by using array value, PHP

how to echo only one value either the value is multiple [PHP]

How to split an array of objects into subarrays depending on the field value in Rails and Mongodb

Using PHP in need two array value in one array

how to round value to only 3 decimal points?

How to get round value if value is in decimal in php?

How to output array with subarrays with same repeating element into XML with PHP

PHP Multiply Value in array where has same element in multidimentional array