Refer to a value just set in array within foreach loop

Mike Rampling

We've got a foreach loop that one of our console commands, it builds an array to be put in the database after being turned to Json.

In order to get one of the specific values, we'd like to reference two of the OTHER values that have been pulled in the loop

Below is the loop, the 'conversionRate' valus is what we're trying to get, from the result of the 'sales' and 'quotes' queries. I have no idea how to call those while still inside the loop.

 foreach ($users as $user) {
            $todaysCcActionsArray[] = [
                'name' => DB::Table('sw_users')->where('EmailAddress', $user)->value('FirstName'),

                'code' => $user,

                'sales' => Order::where('Status', 'BOOKING')
                ->whereNotIn('Product', ['commercial_insurance', 'home_insurance'])
                ->where('MasterOrderNumber', 0)
                ->whereNull('OriginalOrderNumber')
                ->where('CallCentreID', '!=', $user)
                ->whereDate('OrderDate', '=', date('Y-m-d'))->count(),

                'quotes' => Order::where('Status', 'QUOTE')
                ->whereNotIn('Product', ['commercial_insurance', 'home_insurance'])
                ->where('MasterOrderNumber', 0)
                ->whereNull('OriginalOrderNumber')
                ->where('CallCentreID', '!=', $user)
                ->whereDate('OrderDate', '=', date('Y-m-d'))->count(),

                'conversionRate' => 'sales' / 'quotes' * 100
            ];
        }

UPDATE

This Is how it comes out if printed.

array:16 [ 
        0 => array:4 [ 
                "name" => "Melissa" 
                "code" => "mkingnew" 
                "sales" => 2 
                "quotes" => 1 
                ] 
        1 => array:4 [ 
                "name" => "Wendy" 
                "code" => "wjonesnew" 
                "sales" => 2 
                "quotes" => 1 
                ] 
        2 => array:4 [ 
                "name" => "Sarah" 
                "code" => "sdickersonnew" 
                "sales" => 2 
                "quotes" => 1 ] 
David

What you're effectively doing is trying to create an array entry using values from itself whilst you're creating it... you can't do that :)

What you can do is build the entry in pieces, then add it to the array:

foreach ($users as $user) {
    $entry = array(
        'name' => DB::Table('sw_users')->where('EmailAddress', $user)->value('FirstName'),
        'code' => $user,
        'sales' => Order::where('Status', 'BOOKING')
            ->whereNotIn('Product', ['commercial_insurance', 'home_insurance'])
            ->where('MasterOrderNumber', 0)
            ->whereNull('OriginalOrderNumber')
            ->where('CallCentreID', '!=', $user)
            ->whereDate('OrderDate', '=', date('Y-m-d'))->count(),

        'quotes' => Order::where('Status', 'QUOTE')
            ->whereNotIn('Product', ['commercial_insurance', 'home_insurance'])
            ->where('MasterOrderNumber', 0)
            ->whereNull('OriginalOrderNumber')
            ->where('CallCentreID', '!=', $user)
            ->whereDate('OrderDate', '=', date('Y-m-d'))->count(),
    );
    $entry['conversionRate'] = $entry['sales'] / $entry['quotes'] * 100; 

    $todaysCcActionsArray[] = $entry;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

concatinating two array elements with a string within a foreach loop

分類Dev

Add links within a foreach loop

分類Dev

VueJS: How to use a binding value within a for loop of an array?

分類Dev

Set a class on an image within a loop

分類Dev

Get a specific element within forEach loop

分類Dev

Slow .value function within for loop

分類Dev

Updating ProgressBar value within a for loop

分類Dev

The array is updated within the loop but outside the loop is empty

分類Dev

php foreach loop access value outside loop

分類Dev

Max value within array of objects

分類Dev

Capture Next Value for array in foreach

分類Dev

PHP: Set object properties inside a foreach loop

分類Dev

Getting just one value from the array

分類Dev

increment the values of foreach within ifelse loop in C#

分類Dev

C# Make async http requests within a foreach loop

分類Dev

PHP - Echo array value foreach array count

分類Dev

Can foreach loop contain value as an object?

分類Dev

Print value from foreach, for or while loop

分類Dev

PHP Set a value once in a loop

分類Dev

How to loop array of objects using forEach?

分類Dev

Display 2 Array values in foreach() loop - PHP

分類Dev

(PHP) Accessing specific item in array with foreach loop

分類Dev

Insert array connected to foreach loop into database

分類Dev

After set array into foreach cant count that?

分類Dev

array doesn't iterate value just array name

分類Dev

How find a value within an array, with a users input

分類Dev

Getting actual value of an array in loop

分類Dev

Adding array object to an array in a foreach loop if a condition is satisfied in php

分類Dev

Value of variable set in 'while read' loop looses value outside the loop

Related 関連記事

ホットタグ

アーカイブ