0

I am trying to return the values $jsonTurn2Data ['TurnaroundTime(days)'] and $jsonTurn2Data ['count'] into 2 seperate arrays from the function getTurnATime2ByWeek.

Now $jsonDataTurn2Array[] stores the values as a single array. I want to seperate both, can anyone help ?

$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;
}
Wings2fly
  • 887
  • 1
  • 11
  • 31

2 Answers2

0

You can define 2 variables in the calling function such as $jsonTurn2DataTurnaroundTime and $jsonTurn2DataCount in the caller and pass them by reference to the function getTurnATime2ByWeek(). See the function definition with the & sign that denotes pass by reference

Caller code:

    getTurnATime2ByWeek ($wwTurnMinus2, $wwTurnMinus1, $jsonTurn2DataTurnaroundTime, $jsonTurn2DataCount  );


Function definition:

   function getTurnATime2ByWeek($startTime_turn, $endTime_turn, &$jsonTurn2DataTurnaroundTime, &$jsonTurn2DataCount) {

// Assign values to the pass by reference variables here

}
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
rameshpa
  • 555
  • 5
  • 5
0

How about instead of returning the object from function and that object would contains the array that you want.

I have modified your function to show how you can do this. See the example

<?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;
}

As you can while returning from the function I have casted object to array. This is also allowed in PHP.

Also see: http://php.net/manual/en/language.types.array.php and Convert PHP object to associative array to have more insights into converting objects to PHP array.

Community
  • 1
  • 1
Samundra
  • 1,869
  • 17
  • 28