php数组在循环期间使用函数

时间:2017-11-02 13:17:53

标签: php arrays multidimensional-array

我有一个数组

array:3 [▼
  "scoring_average" => array:4 [▼
    0 => "76.402"
    1 => "72.05616203703711"
    2 => "68.208"
    3 => 76.0
  ]
  "front_9_scoring_average" => array:4 [▼
    0 => "41.5"
    1 => "36.15309523809525"
    2 => "33.25"
    3 => "38.0"
  ]
  "back_9_scoring_average" => array:4 [▼
    0 => "39.75"
    1 => "36.078605442176865"
    2 => "33.17"
    3 => "38.0"
  ]
]

我需要将每个元素中的4个项目(例如“scoring_average”)传递给执行某些比较的函数,并根据结果返回一个字符串。

Comparison::CompareLower($BottomStat, $AverageStat, $TopStat, 
$playerStat)

$BottomStat = "76.402", $AverageStat = "72.05616203703711", $TopStat = 
"68.208", $playerStat = 76.0

如何循环遍历此数组并将这四个值传递给函数,获取结果然后移动到数组中的下一个数组?

3 个答案:

答案 0 :(得分:1)

用于使用params数组的调用函数:

$result = call_user_func_array('Comparison::CompareLower', $row);

答案 1 :(得分:0)

<?php

$array = array(
    'scoring_average' => array(
        '76.402',
        '72.05616203703711',
        '68.208',
        '76.0'
    ),
    'front_9_scoring_average' => array(
        '41.5',
        '36.15309523809525',
        '33.25',
        '38.0'
    ),
    'back_9_scoring_average' => array(
        '39.75',
        '36.078605442176865',
        '33.17',
        '38.0'
    )
);

// can be transformed into $array as $key=>$single if you need scoring_average, front_9_scoring_average, ... as $key
foreach ($array as $single) {
    $yourResult = Comparison::CompareLower($single[0], $single[1], $single[2], $single[3]);
    // do whatever you like with $yourResult
}

Foreach是您正在寻找的功能。 http://php.net/manual/en/control-structures.foreach.php

答案 2 :(得分:-1)

您可以根据数组使用innerHTML

cleanNode

我不确定这是不是你的意思。