English 中文(简体)
如何通过联系阵列作为职能参数?
原标题:How to pass an associative array as parameters for a function?
  • 时间:2024-02-22 01:40:20
  •  标签:
  • php

我有共同点:

$percentageAndRepData = [
    1 => [
        "main" => [0.65, 0.75, 0.85, 5, 5, 5],
        "assistance" => [0.5, 0.6, 0.7, 10, 10, 10]
    ],
    2 => [
        "main" => [0.7, 0.8, 0.9, 3, 3, 3],
        "assistance" => [0.6, 0.7, 0.8, 8, 8, 6]
    ],
    3 => [
        "main" => [0.75, 0.85, 0.95, 5, 3, 1],
        "assistance" => [0.65, 0.75, 0.85, 5, 5, 5]
    ],
    4 => [
        "main" => [0.1, 0.4, 0.6, 5, 5, 5],
        "assistance" => [0.4, 0.5, 0.6, 5, 5, 5]
    ]
];

我有以下职能:

function liftCalculator ($trainingMax, $firstSetPercentage, $secondSetPercentage,
$thirdSetPercentage, $firstSetReps, $secondSetReps, $thirdSetReps)

用户投入将提供<条码>培训Max,但我希望<条码>、<条码>和<条码>“协助”阵列中与<条码>计算器参数其余部分对应的所有其他内容。

So on "main" I want to pass 0.65 as $firstSetPercentage, 0.75 as $secondSetPercentage, and so on. Then the same on "assistance", and then repeat for every week.

我将穿透阵列作为争执的论据,但找不到允许我用不同方法提出第一个论点的方法。 我发现的所有例子都假定该职能的所有参数来自各阵列。

问题回答

you can use the spread operator (...) https://www.phptutorial.net/php-tutorial/php-spread-operator/ to unpack elements from the array and pass them as prams in your function also i see you have to pass $trainingMax that is not the part of the array

你可以尝试

//your mentioned function
function liftCalculator($trainingMax, $firstSetPercentage, $secondSetPercentage, $thirdSetPercentage, $firstSetReps, $secondSetReps, $thirdSetReps) {
    // ....
}

//your mentioned array
$percentageAndRepData = [
    1 => [
        "main" => [0.65, 0.75, 0.85, 5, 5, 5],
        "assistance" => [0.5, 0.6, 0.7, 10, 10, 10]
    ],
    // ...
];

// mentioned as user input value
$trainingMax = 100; // change value as req.

foreach ($percentageAndRepData as $weekData) {
    // "main" exercises
    liftCalculator($trainingMax, ...$weekData["main"]);

    // "assistance" exercises
    liftCalculator($trainingMax, ...$weekData["assistance"]);
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签