Jump to content

Recommended Posts

Hello guys, I have this array that I want the 'distance' key value to another array if this array 'supplier' matches the other array 'supplier' value.

Array
(
    [0] => Array
        (
            [supplier] => 'ROLAN'
            [distance] => 3.8
        )

    [1] => Array
        (
            [supplier] => 'COOLDRIVE'
            [distance] => 6.2
        )

    [2] => Array
        (
            [supplier] => 'ENGINEERING.'
            [distance] => 14.6
        )

    [3] => Array
        (
            [supplier] => 'TEST DEPO'
            [distance] => 9.0
        )

)

The other array is So, I just want to add 'distance' from the top array to below which I did already with the foreach loop.

Array
(
    [0] => Array
        (
            [supplier] => ENGINEERING
            [rolanID] => Array
                (
                    [0] => 51154
                )
        )

    [1] => Array
        (
            [supplier] => COOLDRIVE
            [rolanID] => Array
                (
                    [0] => 2338117
                )
        )

    [2] => Array
        (
            [supplier] => ROLAN
            [rolanID] => Array
                (
                    [0] => 2338117
                    [1] => 51154
                )

        )

)

So, Is there any best way than this below my code: Thank you

foreach ($results as $row) {  // this is second array loop
            $supplierAvailable[$row['supplier']]['supplier'] = $row['supplier'];                      
            $supplierAvailable[$row['supplier']]['rolanID'][] = $row['rolan_part_id'];
            
            foreach ($supplierDetails as $index => $depo) { // this is  the first array loop to add distance to second array
                if ($depo['supplier'] == "'".$row['supplier']."'") {
                    $supplierAvailable[$row['supplier']]['distance'] = $depo['distance'];
                    unset($depo[$index]);
                    break;
                }
            }
        }

 

Turn your first array into a map, so it would look like this:

Array
(
    [ROLAN] => 3.8
    [COOLDRIVE] => 6.2
    [ENGINEERING.] => 14.6
    [TEST DEPO] => 9
)

You can use a loop or array_column to accomplish that.

Then you can simply look up the supplier in that map by accessing the appropriate index rather than having to loop and search for it.

$supplierAvailable[$row['supplier']]['distance'] = $distanceMap[$row['supplier']]

 

Edited by kicken
On 8/18/2022 at 10:27 AM, kicken said:

Turn your first array into a map, so it would look like this:

Array
(
    [ROLAN] => 3.8
    [COOLDRIVE] => 6.2
    [ENGINEERING.] => 14.6
    [TEST DEPO] => 9
)

You can use a loop or array_column to accomplish that.

Then you can simply look up the supplier in that map by accessing the appropriate index rather than having to loop and search for it.

$supplierAvailable[$row['supplier']]['distance'] = $distanceMap[$row['supplier']]

 

Hey Kicken, actually the array 'supplier' value has a single quote like below

Array
(
    [0] => Array
        (
            [supplier] => 'ROLAN'
            [distance] => 3.8
        )

so, the it does not match the following code as ROLAN is not equal to 'ROLAN'

$supplierAvailable[$row['supplier']]['distance'] = $distanceMap[$row['supplier']]

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.