从php中的函数返回mutliple数组

时间:2016-04-04 03:28:33

标签: php arrays multidimensional-array

我正在尝试将值$jsonTurn2Data ['TurnaroundTime(days)']$jsonTurn2Data ['count']从函数getTurnATime2ByWeek返回到2个单独的数组中。

现在$ jsonDataTurn2Array []将值存储为单个数组。 我想分开两个人,有人可以帮忙吗?

$jsonDataTurn2Array = [ ];
$jsonDataTurn2Array[] = getTurnATime2ByWeek ($wwTurnMinus2, $wwTurnMinus1 );
$jsonDataTurn2Array[] = getTurnATime2ByWeek ($wwTurnMinus1, $wwTurnNow );



function getTurnATime2ByWeek($startTime_turn, $endTime_turn) {
    global $db;
    $jsonTurn2Data = [ ];

    $qarea = $db->prepare ( "CALL spturnaroundtime(?,?)" );
    $qarea->bindParam ( 1, $startTime_turn );
    $qarea->bindParam ( 2, $endTime_turn );
    $qarea->execute (); 
    $row1 = $qarea->fetchAll ();


    $SumDuration = 0;
    $countRecipe = 0;


    foreach ( $row1 as $r1 ) {

        $SumDuration += $r1 ['Duration'];
        $countRecipe ++;
    }
    if ($countRecipe > 0) {
        $jsonTurn2Data ['TurnaroundTime(days)'] = intval ( $SumDuration / $countRecipe );
        $jsonTurn2Data ['count'] =$countRecipe;
    } 
    else{
        $jsonTurn2Data ['TurnaroundTime(days)'] = intval ( $SumDuration );
        $jsonTurn2Data ['count'] =0;
    }
    return $jsonTurn2Data;
}

2 个答案:

答案 0 :(得分:0)

您可以在调用函数中定义2个变量,例如调用者中的$jsonTurn2DataCountgetTurnATime2ByWeek(),并通过引用函数 getTurnATime2ByWeek ($wwTurnMinus2, $wwTurnMinus1, $jsonTurn2DataTurnaroundTime, $jsonTurn2DataCount ); Function definition: function getTurnATime2ByWeek($startTime_turn, $endTime_turn, &$jsonTurn2DataTurnaroundTime, &$jsonTurn2DataCount) { // Assign values to the pass by reference variables here } 传递它们。使用&amp ;;查看功能定义。表示通过引用传递的标志

来电者代码:

text = seccure.encrypt(b'message\n', b'coR5M|FpR*Vn')

答案 1 :(得分:0)

如何而不是从函数返回对象,而该对象将包含您想要的数组。

我修改了你的功能,以展示你如何做到这一点。参见示例

<?php
$wwTurnMinus2 = 200;
$wwTurnMinus1 = 100;
$jsonDataTurn2Array = [ ];
$jsonDataTurn2Array[] = getTurnATime2ByWeek ($wwTurnMinus2, $wwTurnMinus1 );

var_dump($jsonDataTurn2Array);
// $jsonDataTurn2Array[] = getTurnATime2ByWeek ($wwTurnMinus1, $wwTurnNow);



function getTurnATime2ByWeek($startTime_turn, $endTime_turn) {
    // global $db;
    $jsonTurn2Data = new stdClass();

    // $qarea = $db->prepare ( "CALL spturnaroundtime(?,?)" );
    // $qarea->bindParam ( 1, $startTime_turn );
    // $qarea->bindParam ( 2, $endTime_turn );
    // $qarea->execute (); 
    // $row1 = $qarea->fetchAll ();

    $row1 = [
        [
        'Duration'=>10,
        'TurnaroundTime(days)'=>10,
        'count'=>50
        ],
        [
        'Duration'=>20,
        'TurnaroundTime(days)'=>5,
        'count'=>5
        ]
    ];


    $SumDuration = 0;
    $countRecipe = 0;

    foreach ( $row1 as $r1 ) {
        $SumDuration += $r1 ['Duration'];
        $countRecipe ++;
    }

    if ($countRecipe > 0) {
        $jsonTurn2Data ->turn_around_time_days = intval ( $SumDuration / $countRecipe );
        $jsonTurn2Data->count =$countRecipe;
    } 
    else{
       $jsonTurn2Data->turn_around_time_days = intval ( $SumDuration     );
        $jsonTurn2Data->count =0;
    }
    return (array)$jsonTurn2Data;
}

从函数返回时我可以将对象转换为数组。 PHP中也允许这样做。

另见: http://php.net/manual/en/language.types.array.phpConvert PHP object to associative array 有关将对象转换为PHP数组的更多见解。