Home > Software design >  I want to return the next value each time in $values ​with for
I want to return the next value each time in $values ​with for

Time:06-14

I am trying to export with Laravel. There is an array and it returns 2 values. As:

items: array:2 [▼
    0 => "90"
    1 => "600"
  ]

I want to show these two values ​​in excel, but it always says 90, how can I do it?

for ($i = 0; $i <count($this->values); $i  ) {
    $values[$i]=$this->values[$i];
        
    return [
            //$videohistory->user_id,
            //emoji_decode($videohistory->publisher->nick_name),
            date("Y-m-d H:i:s", $videohistory->create_time),
            date("Y-m-d H:i:s", $videohistory->end_time),
            diff_date_format($videohistory->begin_time, $videohistory->end_time, "%H sa %i dk. %s sn"),
            $values[$i],
        ];    
}

CodePudding user response:

Something like this maybe, collecting the data in an array and only returning it after the loop has completed.


public function map($videohistory): array{

    $ret = [];

    for ($i = 0; $i <count($this->values); $i  ) {
        $values[$i]=$this->values[$i];
            
        $ret[] = 
                [
                //$videohistory->user_id,
                //emoji_decode($videohistory->publisher->nick_name),
                date("Y-m-d H:i:s", $videohistory->create_time),
                date("Y-m-d H:i:s", $videohistory->end_time),
                diff_date_format($videohistory->begin_time, $videohistory->end_time, "%H sa %i dk. %s sn"),
                $values[$i],
                ];    
    }
    return $ret;
}

CodePudding user response:

  1. As @aynber stated, you're trying to return in your for loop. What that actually does is it just exits the function you're in and returns the value. As stated in the PHP Documentation:

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call.

Look for more information in the PHP Documentation


Solution:

You may apply an array_map().


What does array_map() do?

array_map(callback, ...arrays)

The function iterates through your arguments (it takes the value from your arrays).

For example, if you give it two arrays:

$myArr1 = ['hello' => 'world']; 

and

$myArr2 = ['foo' => 'bar'];

the callback function will accept 2 arguments

$myResult = array_map(function ($myArr1Value, $myArr2Value) { 
    return 'Any value here';
}, $myArr1, $myArr2);

// output:
// ['Any value here', 'Any value here'];

and will iterate only through array values.


Solution Code:

$items = ["90", "600"];

$myResult = array_map(function ($iterationValue) {
    return [
        //$videohistory->user_id,
        //emoji_decode($videohistory->publisher->nick_name),
        date("Y-m-d H:i:s", $videohistory->create_time),
        date("Y-m-d H:i:s", $videohistory->end_time),
        diff_date_format($videohistory->begin_time, $videohistory->end_time, "%H sa %i dk. %s sn"),
        $iterationValue
    ];
}, $items);

$myResult will contain an array of arrays, for example:

print_r($myResult);
//output:
//[ ['date', 'date', "90"], ['date', 'date', "600"] ]; 
  • Related