从多维数值数组中提取数据

时间:2022-08-11 21:29:40

I have 2 dimensional array that is filled with data from the table.

我有2维数组,填充表中的数据。

I would like to extract each value and use it for calculation and save the result into new array.

我想提取每个值并将其用于计算并将结果保存到新数组中。

For example:

例如:

$array[0][0] + 5 =  $result0

$array[0][1] + 35 = $result1

$array[0][2] + 55 = $result2

$array[0][3] + 15 = $result3

$array[0][4] + 75 = $result4

And then repeat the same calculation for whole next array in main array and all the others.

然后对主阵列中的整个下一个阵列和所有其他阵列重复相同的计算。

Array
(
    [0] => Array
        (
            [0] => 1
            [ID] => 1
            [1] => 0
            [name] => 0
            [2] => 0
            [date] => 0
            [3] => 5
            [number] => 5
            [4] => 0
            [size] => 0
        )

    [1] => Array
        (
            [0] => 2
            [ID] => 2
            [1] => 0
            [name] => 0
            [2] => 0
            [date] => 0
            [3] => 3
            [number] => 3
            [4] => 0
            [size] => 0
        )

    [2] => Array
        (
            [0] => 3
            [ID] => 3
            [1] => 0
            [name] => 0
            [2] => 0
            [date] => 0
            [3] => 7
            [number] => 7
            [4] => 0
            [size] => 0
        )

)

1 个解决方案

#1


1  

To extract each element from the two dimensional array use a foreach loop like shown below and Declare a new array and add updated values in the array after changing.

要从二维数组中提取每个元素,请使用如下所示的foreach循环并声明一个新数组,并在更改后在数组中添加更新的值。

$cars = array
      (
      array(1,"id"=>100,"name"=>96),
      array(0,"id"=>60,"name"=>59),
      array(5,"id"=>110,"name"=>100)
      );



$result= array();


  foreach ($cars as $v1) { 
    foreach ($v1 as $v2) { 
        $result[]= $v2+5; 
    } 
} 
  print_r($result);

#1


1  

To extract each element from the two dimensional array use a foreach loop like shown below and Declare a new array and add updated values in the array after changing.

要从二维数组中提取每个元素,请使用如下所示的foreach循环并声明一个新数组,并在更改后在数组中添加更新的值。

$cars = array
      (
      array(1,"id"=>100,"name"=>96),
      array(0,"id"=>60,"name"=>59),
      array(5,"id"=>110,"name"=>100)
      );



$result= array();


  foreach ($cars as $v1) { 
    foreach ($v1 as $v2) { 
        $result[]= $v2+5; 
    } 
} 
  print_r($result);