Quick actions

cmd+k|ctrl+k

Navigation

Languages

How to break nest loop (problems)

Snippet info

Language

Php

Visibility

public

Author

zjmainstay

Created

2016-07-12T01:31:22Z

Updated

2016-07-12T02:13:30Z

<?php

$investList = [
        [
            'id'    => 1,
            'cash'  => 1000,
        ],
        [
            'id'    => 2,
            'cash'  => 4000,
        ],
        [
            'id'    => 3,
            'cash'  => 1000,
        ],
        [
            'id'    => 4,
            'cash'  => 3000,
        ],
        [
            'id'    => 5,
            'cash'  => 2000,
        ],
    ];
    
    $refundList = [
        [
            'invest_id'    => 1,
            'cash'  => 1000,
        ],
        [
            'invest_id'    => 2,
            'cash'  => 4000,
        ],
        [
            'invest_id'    => 3,
            'cash'  => 1000,
        ],
        [
            'invest_id'    => 4,
            'cash'  => 3000,
        ],
        [
            'invest_id'    => 5,
            'cash'  => 2000,
        ],
    ];
    
    foreach($refundList as $index => $refund) {
        foreach($investList as $invest) {
            if($invest['id'] == $refund['invest_id']) {
                $refundList[$index]['invest_cash'] = $invest['cash'];
            }
        }
    }
    
    print_r($refundList);
    
    //When the investList run up to 10000, the loop times can be 10000*10000=100000000, how to optimize this?
    //Solution: https://glot.io/snippets/eggpzzp1tm
    
    
INFO