Jump to content

foreach loop - array overwriting


ajhanna88

Recommended Posts

I'm still pretty new to php and have an issue i'm trying to solve, I have the following two arrays 

 

The first one is contains product id's and the quantity ordered 

Array
(
    [35659] => 1
    [35699] => 1
    [35735] => 2
)

The second one contains warehouse locations that stock the product and the quantity they have available 

Array
(
    [35659] => Array
        (
            [9] => 10
            [114] => 1
            [126] => 0
        )

    [35699] => Array
        (
            [9] => 8
            [114] => 0
            [126] => 5
        )

    [35735] => Array
        (
            [9] => 10
            [114] => 0
            [126] => 0
        )

)

So what I am trying to do is loop through and add to an array each warehouse that meets the quantity of the order here is my code: 

$stockrequired = array('35659' => '1', '35699' => '1', '35735' => '2');

$instock = array();
foreach ($locations as $location) {
    foreach($stockrequired as $id => $qty){
        if($stockavailable[$id][$location] >= $qty){
            $instock[$id] = $location;
        }
    }
}

print_r($instock);

However this produces the following which is the last warehouse that meets the quantity. 

Array
(
    [35659] => 114
    [35699] => 126
    [35735] => 9
)

What I need is all the warehouses the meet the quantity for each product e.g. my desired outcome will be below. I'm guessing my loop is getting reset or something? Any help would be appreciated. 

Array
(
    [35659] => 9
    [35659] => 114

    [35659] => 9
    [35699] => 126

    [35735] => 9
)
Link to comment
Share on other sites

awesome that worked thanks. 

 

I now want to group this array: 

Array
(
    [35659] => 9
    [35659] => 114

    [35659] => 9
    [35699] => 126

    [35735] => 9
)

into

Array
(
    [9] => Array
        (
            [35659] 
            [35659] 
            [35735] 
        )
    [114] = > Array 
        (
            [35659]
        )
    [126] = > Array 
        (
            [35699]
        )





)

e.g. group by warehouse that contains the most products?

 

Thanks

Link to comment
Share on other sites

 

It's similar to above, use a foreach loop and add arrays inside arrays -

$warehouses = array();

foreach($array as $product => $warehouse) {
	$warehouses[$warehouse] = $product;
}

print_r($warehouses);

 

 

Thanks, that worked  :) the final part of my puzzle is now compare the below array product ids

Array
(
[35659] => 1
[35699] => 1
[35735] => 2
)

to this one, I want to loop through until each product id has been matched in the second array.

Array
(
    [9] => Array
        (
            [35659] => 35659
            [35699] => 35699
            [35735] => 35735
        )

    [126] => Array
        (
            [35659] => 35659
            [35699] => 35699
        )

    [114] => Array
        (
            [35659] => 35659
        )

)

what i am trying to produce is just this 

    [9] => Array
        (
            [35659] => 35659
            [35699] => 35699
            [35735] => 35735
        )

Thanks

Edited by ajhanna88
Link to comment
Share on other sites

Post where you get stuck and I'm sure someone can point you in the right direction.

 

You need to at least try something before asking for help.

 

I agree, I have made an attempt however it's not a great one. i don't expect to be given the answer just some advice to get me going on the right lines. 

 

Thanks

//loop through each product in order
foreach ($stockrequired as $id) {
    //loop through all items in stock
    foreach ($instock as $key => $value) {
        //here I want to match the "product ids that are the same in both arrays
        while ($id == $value) {
           //while stock required id is equal to instock id   
        }
    }
}
Edited by ajhanna88
Link to comment
Share on other sites

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.