Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Lambert Traccard
Lambert Traccard
15,142 Points

Problem traversing a multidimensional array and set values during it.

$log = array(
    '#1176' => array(
            'user_id' => 1,
            'order_number' => 129,
            'order_total' => 29.6,
            'points_earned' => 1,
            'points_used' => 0,
            'order_status' => 'completed',
            'date' => '2015-03-02 14:56:00'
        ),

    '#1174' => array(
            'user_id' => 1,
            'order_number' => 127,
            'order_total' => 165,
            'points_earned' => 8,
            'points_used' => 0,
            'order_status' => 'completed',
            'date' => '2015-03-02 14:43:36'
        ),

    '#1173' => array(
            'user_id' => 1,
            'order_number' => 126,
            'order_total' => 24.8,
            'points_earned' => 1,
            'points_used' => 0,
            'order_status' => 'completed',
            'date' => '2015-03-02 15:26:00'
        ),

    '#1172' => array(
            'user_id' => 1,
            'order_number' => 125,
            'order_total' => 39.8,
            'points_earned' => 1,
            'points_used' => 0,
            'order_status' => 'completed',
            'date' => '2015-03-02 15:26:00'
        )
);

I have a variable $points = 10;

Only where 'order_status' == completed, I want to attributes those 10 points until 'points_earned' == 'points_used' && until there is no point left.

I don't know if I made myself clear. I have tried for 2 days now and it's making me crasy !

Thanks for your help.

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points

can you be more specific ? because i don't get the point really.. what do you want to achieve?

1 Answer

Lambert Traccard
Lambert Traccard
15,142 Points
$i = 0;
$points_recq = 10; // require points for a coupon


// if the points left are >= to the require points for a coupon
if ($points_left >= $points_recq) {

    foreach ($log as $orderid => &$event) {

        if($event['order_status'] == 'completed') {

            // attributes points until points used == points earned
            // stop if there is no more points to attribute
            while ($event['points_earned'] > $event['points_used']){
                $event['points_used']++;
                $i++;

                // if there is no point left to attribute 
                if($i >= $points_recq) {break;}
            }
        }

        // if there is no point left to attribute 
        if($i >= $points_recq) {break;}
    }
}

I finally made it this morning, I think a good sleep cleared my mind. Don't know if it's the best way, but it's working. Thanks for your help.

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points

hahah i didn't do anything you came with a solution. I've often the problem that i can't see the problem taking some distance will clear your mind for sure.