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
https://forums.phpfreaks.com/topic/289774-foreach-loop-array-overwriting/
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

 

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

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   
        }
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.