Angel Valdez
Angel Valdez

Reputation: 87

create a multidimensional array

I am working in a hotel reservation system, and the rates are per night. My database has two tables, one called stock and the other promotions. The first contain one row for day (365 days), and the last one only the days that have a percent of discount. Sometimes, some days have two or more different percent of discount. In this case 30 and 31 of january have 0.10 and 0.15.

Table stock

date         | rate
--------------------------
2017-01-29     500
2017-01-30     500
2017-01-31     500

Table promotions

date         | percent
--------------------------
2017-01-30     0.10
2017-01-31     0.10
2017-01-30     0.15
2017-01-31     0.15

While a mysql query (SELECT s.date, s.rate, IFNULL(p.percent, 0) FROM stock s LEFT JOIN promotions p ON s.date = p.date WHERE s.date as date BETWEEN '2017-01-29' AND '2017-01-31') I obtain this result:

date         | rate       | percent
-----------------------------------
2017-01-29     500          0
2017-01-30     500          0.10
2017-01-31     500          0.10
2017-01-30     500          0.15
2017-01-31     500          0.15

I need to create a multidimensional array, that contain one array for each percent of discount, and these arrays contain the three days, the day 29 with a percent 0.00 and the other two days 0.10. a new one the day 29 with a percent 0.00 and the other two days 0.15. I am trying but I can't assign the day 29 in both arrays, like in the example.

Array
(
    [0.10] => Array
        (
            [0] => Array
                (
                    [date] => 2017-01-29
                    [rate]=>500
                    [percent] => 0.00
                )

            [1] => Array
                (
                    [date] => 2017-01-30
                    [rate]=>500
                    [percent] => 0.10
                )

            [2] => Array
                (
                    [date] => 2017-01-31
                    [rate]=>500
                    [percent] => 0.10
                )
        )

    [0.15] => Array
        (
            [0] => Array
                (
                    [date] => 2017-01-29
                    [rate]=>500
                    [percent] => 0.00
                )

            [1] => Array
                (
                    [date] => 2017-01-30
                    [rate]=>500
                    [percent] => 0.15
                )

            [2] => Array
                (
                    [date] => 2017-01-31
                    [rate]=>500
                    [percent] => 0.15

                )
        )
)

I am a little stuck with the php array. I can group the result of the first array by percent, with this code:

$grouped = array();
foreach ($array_p as $item) {
$grouped[$item['percent']][]= $item; 
}

but i only obtain this result

    Array
(
    [0.10] => Array
        (
            [0] => Array
                (
                    [date] => 2017-01-30
                    [rate] => 500
                    [percent] => 0.10
                )

            [1] => Array
                (
                    [date] => 2017-01-31
                    [rate] => 500
                    [percent] => 0.10
                )

        )

    [0.15] => Array
        (
            [0] => Array
                (
                    [date] => 2017-01-30
                    [rate] => 500
                    [porcentaje] => 0.15
                )

            [1] => Array
                (
                    [date] => 2017-01-31
                    [rate] => 743.80
                    [percent] => 0.15
                )

        )

    [0.00] => Array
        (
            [0] => Array
                (
                    [date] => 2017-01-29
                    [rate] => 500
                    [percent] => 0.00
                )

        )

)

How can I insert the three days in each array?

Upvotes: 0

Views: 90

Answers (2)

Jyoti
Jyoti

Reputation: 45

Try This--

              $grouped = array();
              $seperate = array();
                foreach ($array_p as $item) {
                    if($item["percent"] == 0) {
                      $seperate[] = $item;
                    } else {                          
                        $grouped[$item['percent']][]= $item;
                    }
                }
                foreach( $grouped as $key => $val ) {
                    $grouped[$key] = array_merge($val,$seperate);
                }
                print_R($grouped);

Upvotes: 1

Condorcho
Condorcho

Reputation: 503

I'm not sure this is the best way to do it, but it may help you get a start:

$grouped = array();

foreach ($array_p as $item) {
    $grouped[$item['percent']] = array();
}

foreach ($array_p as $item) {
    if (!in_array($array_p[0], $grouped[$item['percent']]) && $item['percent'] != 0) {
        $grouped[$item['percent']][] = $array_p[0];
    }

    $grouped[$item['percent']][] = $item;
}

This is assuming that discount 0 is always at index 0 in $array_p, and that you will always store discount 0. Anyway, i'll try to improve it.


EDIT: I improved the above answer with another way using only one foreach loop

$grouped = array();

foreach ($array_p as $item) {
    if (!array_key_exists($item['percent'], $grouped) {
        $grouped[$item['percent']] = array();
    }

    if (!in_array($array_p[0], $grouped[$item['percent']]) && $item['percent'] != 0) {
        $grouped[$item['percent']][] = $array_p[0];
    }

    $grouped[$item['percent']][] = $item;
}

NOTE: These methods will not save duplicated items.

Documentation:

Upvotes: 0

Related Questions