tsompanis
tsompanis

Reputation: 344

Count duplicates in array and work with other data in it

So i have an array in codeigniter view witch contains data from Controller<->Model.

The array it's like this


array(30) (
    [0] => array(37) (
        [idbook] => numstring '463'  (length=3)
        [idcoupon] => numstring '0'  (length=1)
        [idroom] => numstring '39'  (length=2)
        [idcategory] => numstring '2'  (length=1)
        [idguests] => numstring '68'  (length=2)
        [nguests] => numstring '4'  (length=1)
        [value_paid] => numstring '0'  (length=1)
        [value_pending] => numstring '275'  (length=3)
        [value_full] => numstring '275'  (length=3)
        [valid_from] => string '2015-06-15' (length=10)
        [valid_to] => string '2015-06-20' (length=10)
        [today] => string '2014-11-17' (length=10)
        [extra_ids] => string '' (length=0)
        [nchilds] => numstring '4'  (length=1)
        [preferences] => string '' (length=0)
        [status] => numstring '2'  (length=1)
        [lchilds] => numstring '4'  (length=1)
        [pay_method] => numstring '1'  (length=1)
        [cardnumber] => string '' (length=0)
        [cardname] => string 'Visa' (length=4)
        [exp_month] => numstring '1'  (length=1)
        [exp_year] => numstring '2010'  (length=4)
        [arrival_time] => numstring '0'  (length=1)
        [name] => string 'Aland Islands' (length=13)
        [cost] => numstring '50'  (length=2)
        [facilities] => string '***********' (length=298)
        [description] => string '***********' (length=656)
        [url] => string 'appartment.JPG' (length=14)
        [idcountry] => numstring '2'  (length=1)
        [surname] => string 'R***' (length=7)
        [email] => string '*****' (length=20)
        [city] => string 'hffghfh' (length=7)
        [addr] => string 'dfhgfdgh  ' (length=10)
        [phone] => numstring '34****5'  (length=7)
        [alt_phone] => string '' (length=0)
        [title] => string 'Mr' (length=2)
        [birthdate] => null
    )

This get's bigger as there are more reservations for, let's say, year 2015, as you will notice from the first line: Array(30)

What i want to do is: I have to count how many reservations are from the same country (ie: Germany) AND for each one of those results (let's say they are 10) sum all the guests (nguests+nchilds+lchilds), also i have to find how many overnights they have for each reservation for each Country.

I've got the "function" to find how many days they have spend in the Hotel. But i cant find a way, to sepperate the results for each country, without this getting displayed two or more times. (ie Germany print's more than 1 time when i echo it)

Any help, snipets, hints, ideas are more than welcome!

Upvotes: 0

Views: 40

Answers (1)

Mike Miller
Mike Miller

Reputation: 3129

So I assume you will identify Germany (or any other country) using the 'id_country' field? Regardless, you can adjust the following to suit your needs...

foreach($the_array as $sub_array){
    //this creates an entry in the result indexed by the country id
    $results[$sub_array['id_country']]['reservations']++;//adds to the count for the 'reservations' key within the array indexed by country id
    //this will add the count of guest to an existing item or create a new one if none exists
    $results[$sub_array['id_country']]['guests']+=($sub_array['nguests']+$sub_array['nchilds']+$sub_array['lchilds'])none exists
}

Upvotes: 1

Related Questions