Jump to content

Seeing forward in array


Go to solution Solved by kicken,

Recommended Posts

I'm trying to "see ahead" in my associative array  This hypothetical is the simplest way to describe what I'm trying to accomplish.

I have an array that looks like this

$arr = [ 0 => ["meds" => [ "IV" => 1, "pill" => 2, "syrup" = 3] ],
         1 => ["meds" => [ "IV" => 2, "pill" => 4, "syrup" = 6] ],
         2  => ["meds" => [ "IV" => 5, "pill" => 5, "syrup" = 5] ]
];

We have 3 rooms for patients that each contain the designated medication types.

Using foreach() I can successfully run through the array.

Example: if IV quantity is less than 3, create a message to order the amount that will bring inventory up to 3.

Obviously this can be done uniformly.

My problem: I want to "reach ahead" to affect a current element.

Ex: while processing the order for IVs, if pills are less than 10 then do something (double the order, print in red) to IVs. Likewise, if pills are needed, do something, but do something more based on syrup.

I've fumbled the idea of creating a new array and flipping the contents around, but nothing seems to be resolving my issue.

Link to comment
https://forums.phpfreaks.com/topic/326784-seeing-forward-in-array/
Share on other sites

Something doesn't make sense here. Why can you look at IV values in the "current" element but for the pills/syrup you have to look "ahead"?

I'm also not really following the algorithm. Given the example there, exactly what steps are you (as a human) following to get exactly what output?

Envision this:

IVs can be used to replace pills, but are more expensive than pills.

Pills can be used to replace syrup, but are more expensive than syrup.

Ideally, each room has a supply of 2 IVs, 4  pills, and 8 syrup.

However, if an item falls below a threshold, it may indicate a supply shortage and therefore triggers a bulk-up of the higher teir item for an overstocking response (with a ceiling for the next cycle).

Due to budget, only one overstock event of 10 units per room is allowed per cycle.

Given these parameters, room 1 with 2 IVs and 4 pills would order no IVs or pills.  However, once the syrup deficit is realized, the pill order needs to shift to 10 for overstock. No syrup is ordered due to budget, but a notification is sent to the procurement team. Hence, pills is reliant on "looking ahead" at syrup.

Room 0 has one of 2 IVs. With that assessment, an order of 1 would be expected. But bc pills is below 4, an overstock is triggered for 10 IVs for room 0. Nothing else will be ordered for room 0 in this cycle.

This is why there is a need to "see forward" in the array.

  • Solution

It doesn't sound like you are looking ahead into other rooms, just looking at the stock levels of all the supplies in the current room.  Ex:

foreach ($roomList as $room){
    $ivStockLow = $room['meds']['IV'] < 2;
    $pillStockLow = $room['meds']['pill'] < 4;
    $syrupStockLow = $room['meds']['syrup'] < 8;

    $ivOrder = max(0,2-$room['meds']['IV']);
    $pillOrder = max(0,4-$room['meds']['pill']);
    $syrupOrder = max(0,8-$room['meds']['syrup']);

    if ($pillStockLow){
        $ivOrder=10;
    } else if ($syrupStockLow){
        $pillOrder=10;
    }
}
                                                 

Determine whether each item is low first, then use that information to adjust the order amount later on.  Add in whatever constraints you need for budget as well.

  • Like 1

From the looks of it, you are simply unclear on how PHP arrays work, and the syntax involved.

PHP arrays are highly versatile, in that they combine what in many other languages, requires multiple different data structure types.   It is a combination of an Array, a List and a Map, if we were going back to the standard library of C++.

Maps are probably the most interesting, in that a map is a structure that has a 'key' = 'value' association.  This is different from an array, which is simply a data structure of 1-N elements, indexed numerically.  In c and c++ arrays are limited to a data type.

Due to strict typing requirements in c and c++ (and other similar "strongly typed") languages, you are constrained in how you assemble, add to and modify an array, but PHP is extremely flexible in essentially letting you store data type or object as a value in an array, and allowing you to nest arrays within arrays any way that suits you.

In your example, you presented a series of nested arrays, some being traditionally numeric, and others containing one or more "key/value" elements.

The curious thing is that the array you presented is invalid.  Notice that your syrup values are missing the => required to define a key/value element.

$arr = [ 0 => ["meds" => [ "IV" => 1, "pill" => 2, "syrup" = 3] ],
         1 => ["meds" => [ "IV" => 2, "pill" => 4, "syrup" = 6] ],
         2  => ["meds" => [ "IV" => 5, "pill" => 5, "syrup" = 5] ]
];

 

I fixed that for the following example code. 

Rather than defining this in one shot, consider how you could start with an empty array and arrive at the same structure.

 

<?php

function printArr($a, $name='') {
    echo "$name --------------------------\n\n";
    print_r($a);
    echo PHP_EOL;
}


$arr = [ 0 => ["meds" => [ "IV" => 1, "pill" => 2, "syrup" => 3] ],
         1 => ["meds" => [ "IV" => 2, "pill" => 4, "syrup" => 6] ],
         2  => ["meds" => [ "IV" => 5, "pill" => 5, "syrup" => 5] ]
];

printArr($arr, 'Original Array');


// Empty
$arr2 = [];
printArr($arr2, 'Rebuilt Array');

$arr2[0] = [];
printArr($arr2);

$arr2[0]['meds'] = [];
printArr($arr2);

$arr2[0]['meds']['IV'] = 1;
printArr($arr2);

$arr2[0]['meds']['pill'] = 2;
printArr($arr2);

$arr2[0]['meds']['syrup'] = 3;
printArr($arr2);

// Add a new empty array to 1st dimenion of array.  This will become $arr2[1]
$arr2[] = [];
printArr($arr2, '1st Dimension, new index 1. Arrays are zero based');

// Assign value to be new array with key "meds" having a value of an empty array.
$arr2[1]['meds'] = [];
printArr($arr2);

// Set the value of this nested array element to be a nested array
$arr2[1]['meds'] = [ "IV" => 2, "pill" => 4, "syrup" => 6];
printArr($arr2);

//Set entire element 3 structure in one assignment
$arr2[] = ['meds' => [ "IV" => 5, "pill" => 5, "syrup" => 5]];
printArr($arr2, 'Array complete');


echo "Comparisons ---------------------\n";
echo $arr[0]['meds']['pill'] . PHP_EOL;
echo $arr2[0]['meds']['pill'] . PHP_EOL;

echo "\n\n";

foreach($arr[2]['meds'] as $nestedKey => $nestedVal) {
    echo "$nestedKey = $nestedVal\n";
}
echo "\n\n";

foreach($arr2[2]['meds'] as $nestedKey => $nestedVal) {
    echo "$nestedKey = $nestedVal\n";
}


The Results:

Original Array --------------------------

Array
(
    [0] => Array
        (
            [meds] => Array
                (
                    [IV] => 1
                    [pill] => 2
                    [syrup] => 3
                )

        )

    [1] => Array
        (
            [meds] => Array
                (
                    [IV] => 2
                    [pill] => 4
                    [syrup] => 6
                )

        )

    [2] => Array
        (
            [meds] => Array
                (
                    [IV] => 5
                    [pill] => 5
                    [syrup] => 5
                )

        )

)

Rebuilt Array --------------------------

Array
(
)

 --------------------------

Array
(
    [0] => Array
        (
        )

)

 --------------------------

Array
(
    [0] => Array
        (
            [meds] => Array
                (
                )

        )

)

 --------------------------

Array
(
    [0] => Array
        (
            [meds] => Array
                (
                    [IV] => 1
                )

        )

)

 --------------------------

Array
(
    [0] => Array
        (
            [meds] => Array
                (
                    [IV] => 1
                    [pill] => 2
                )

        )

)

 --------------------------

Array
(
    [0] => Array
        (
            [meds] => Array
                (
                    [IV] => 1
                    [pill] => 2
                    [syrup] => 3
                )

        )

)

1st Dimension, new index 1. Arrays are zero based --------------------------

Array
(
    [0] => Array
        (
            [meds] => Array
                (
                    [IV] => 1
                    [pill] => 2
                    [syrup] => 3
                )

        )

    [1] => Array
        (
        )

)

 --------------------------

Array
(
    [0] => Array
        (
            [meds] => Array
                (
                    [IV] => 1
                    [pill] => 2
                    [syrup] => 3
                )

        )

    [1] => Array
        (
            [meds] => Array
                (
                )

        )

)

 --------------------------

Array
(
    [0] => Array
        (
            [meds] => Array
                (
                    [IV] => 1
                    [pill] => 2
                    [syrup] => 3
                )

        )

    [1] => Array
        (
            [meds] => Array
                (
                    [IV] => 2
                    [pill] => 4
                    [syrup] => 6
                )

        )

)

Array complete --------------------------

Array
(
    [0] => Array
        (
            [meds] => Array
                (
                    [IV] => 1
                    [pill] => 2
                    [syrup] => 3
                )

        )

    [1] => Array
        (
            [meds] => Array
                (
                    [IV] => 2
                    [pill] => 4
                    [syrup] => 6
                )

        )

    [2] => Array
        (
            [meds] => Array
                (
                    [IV] => 5
                    [pill] => 5
                    [syrup] => 5
                )

        )

)

Comparisons ---------------------
2
2


IV = 5
pill = 5
syrup = 5


IV = 5
pill = 5
syrup = 5

 

If you can look at these results and understand them, you'll have a better idea of how to reference any element in a nested array directly.  You can also reference any dimension, going from left to right, where the first Dimension (array) will be $r[], the 2nd dimension $r[][] and so on, for as many nested dimensions as you might have.

  • Like 1

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.