0

I have two arrays one with conversion_offer_id, conversions & payout. Now i want to match it with another array and add all the values of first array to second array and if not then add values of conversion & payout as 0.

Array 1 ($conversions)

[
{
"conversion_offer_id": 90,
"conversions": 15,
"payout": 225
},
{
"conversion_offer_id": 196,
"conversions": 1,
"payout": 120
},
{
"conversion_offer_id": 720,
"conversions": 53,
"payout": 1590
}
] 

Array 2 ($clkcs)

[
{
"clicks": 142,
"offer_currency": 45,
"click_offer_id": 90,
"click_affiliate_id": 9106,
"offer_name": "BKS",
"offer_id": 90,
"currency_symbol": "₹"
},
{
"clicks": 8,
"offer_currency": 45,
"click_offer_id": 196,
"click_affiliate_id": 9106,
"offer_name": "FFU",
"offer_id": 196,
"currency_symbol": "₹"
},
{
"clicks": 1,
"offer_currency": 45,
"click_offer_id": 246,
"click_affiliate_id": 9106,
"offer_name": "GFT",
"offer_id": 246,
"currency_symbol": "₹"
},
{
"clicks": 1,
"offer_currency": 45,
"click_offer_id": 334,
"click_affiliate_id": 9106,
"offer_name": "JPR",
"offer_id": 334,
"currency_symbol": "₹"
},
{
"clicks": 1,
"offer_currency": 45,
"click_offer_id": 622,
"click_affiliate_id": 9106,
"offer_name": "TWS",
"offer_id": 622,
"currency_symbol": "₹"
},
{
"clicks": 1197,
"offer_currency": 45,
"click_offer_id": 720,
"click_affiliate_id": 9106,
"offer_name": "KPS",
"offer_id": 720,
"currency_symbol": "₹"
}
]

My Logic to achieve

foreach($clkcs as $k=>$key){
            foreach($conversions as $ckey){
                if($key['click_offer_id']==$ckey['conversion_offer_id']){

                    $clkcs[$k]['offer_id']=$ckey['conversion_offer_id'];

                    $clkcs[$k]['conversions']=$ckey['conversions'];
                    $clkcs[$k]['payout']=$ckey['payout'];

                }else{
                    $clkcs[$k]['conversions']='0';
                    $clkcs[$k]['payout']='0';

                }
            }

        }

Conversions & payout for only last matching values are populating fine. For rest of them its 0.

But it should also add the conversion & payout for offer ID 90 & 196.

What i am doing wrong?

3 Answers 3

2

You need stop second foreach after success filling of $clkcs

foreach($clkcs as $k=>$key){
    $clkcs[$k]['conversions']='0';
    $clkcs[$k]['payout']='0';

    foreach($conversions as $ckey){
        if($key['click_offer_id']==$ckey['conversion_offer_id']){
            $clkcs[$k]['offer_id']=$ckey['conversion_offer_id'];

            $clkcs[$k]['conversions']=$ckey['conversions'];
            $clkcs[$k]['payout']=$ckey['payout'];

            break;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You repopulate array every time you foreach $clkcs.

So after setting right conversations for ID 90 in first run in next one you set conversions = 0 and payout = 0 for ID 90 again.

Now you should find right way to do it :)

If you like to make it faster and easier (and conversion_offer_id is unique) add it as $conversions[conversion_offer_id] and iterate only one array.

1 Comment

Id depends what your script needs are. If only one match is possible you can do it very fast by checking if 'conversions' is set already (!=0) and not reporulate it again.
0

The data to be appended is identified by a column of integers and does not risk encountering key collisions or data loss. Declare a lookup array by defining first-level keys by the identifying column. Then iterate the other array and efficiently access the related data or fallback to a zero value while appending the desired elements. Using key-based searches (in this way) will be more performant than brute-force value-based searches (nested loops). Demo

$map = array_column(
    $conversions,
    null,
    'conversion_offer_id'
);

var_export(
    array_map(
        fn($row) => $row
            + [
                'conversions' => $map[$row['offer_id']]['conversions'] ?? 0,
                'payout' => $map[$row['offer_id']]['payout'] ?? 0,
            ],
        $clicks
    )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.