Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 19
  1. #1

    Somma di alcuni valori in un array

    Ho un array formato in questo modo

    ID ROOM, ID TRATTAMENTO, PREZZO

    852, 1, 400
    852, 1, 250

    852, 2, 300
    852, 2, 500

    852, 3, 500
    852, 3, 600

    853, 1, 500
    853, 1, 600

    Quello che devo fare io + sommare il valore PREZZO per array che hanno uguale ID ROOM e ID TRATTAMENTO.
    Il tutto sta all'interno dello stesso array multidimensionale.

  2. #2
    Moderatore di Javascript L'avatar di ciro78
    Registrato dal
    Sep 2000
    residenza
    Napoli
    Messaggi
    8,514
    ma i dati li estrai da un database?
    Ciro Marotta - Programmatore JAVA - PHP
    Preferisco un fallimento alle mie condizioni che un successo alle condizioni altrui.


  3. #3
    Non proprio. Insomma non posso fare un calcolo quando formo l'array. devo sommarli così.
    Vi posto il codice




    Codice PHP:

    foreach($pack->id_packages as $id_package){
              
                
    //vedo se ci sto almeno un giorno in questo package
              
    if($pack->GetDaysInPackage($id_package) > 0){
               foreach(
    $rm->GetRooms($id_package) as $id_room){

                        
                   
    $reduction = new reductions();
                   
    $reduction->People($id_package$id_room$id_treatment['id_treatment'], $pack->GetDaysInPackage($id_package), $inquire);
                    
            
                       
                }
              }
             
           }

          
    $price_adults $inquire->GetPriceAdults();
            foreach(
    $price_adults as $price){
                
    $rm->GetInfo($price[room]);
                
    $tr->GetInfo($price[treatment_type]);
                echo 
    "

    Camera \"" 
    $rm->GetName() . "\" in trattamento \"" $tr->GetName() . "\" ha un costo di  " $inquire->PrintPrice($price[price]) . " euro </p>";
            } 
    Codice PHP:
          foreach($treatments_type as $treatment_type){
                ++
    $a;
                 for(
    $i 1;$i<= $inquire->GetTotalPeople(); $i++){
                
                  
    //echo "

    <u>(ROOM $id_room)</u>[b]Trattamento$treatment_type [/b]</p>";
                  unset(
    $this->id_reductions);
                  
    $this->SearchReduction($id_package$id_room,  $treatment_type$i$inquire->GetAgePerson($i));
                       
                  
    $treatments = $rm->GetBasePrice($id_package$id_room$treatment_type);

                    foreach(
    $this->id_reductions as $id_reduction){

                      if(
    $id_reduction !== 'NO'){
                           
    $this->GetInfo($id_reduction);
                           
    $price_adults[$treatment_type][$id_room] += $this->CalculatePriceTreatment($treatments['base_price'], $this->GetPrice(), $this->GetOperator());

                       }else{
                           
    $price_adults[$treatment_type][$id_room] += $treatments['base_price'];
                       }

                       
                   }
             
               }


                        
    $price_adults[$treatment_type][$id_room] = $price_adults[$treatment_type][$id_room]/7 * $days;
                        
    $inquire->SetPriceAdults($id_room$treatment_type$price_adults[$treatment_type][$id_room]);
         } 
    Spero riusciate a capirlo

  4. #4
    Scusa non ho letto il tuo codice, rispondo solo alla domanda iniziale:

    Codice PHP:
    <?php
    $data
    [] = array(8521400);
    $data[] = array(8521250);
    $data[] = array(8522300);
    $data[] = array(8522500);
    $data[] = array(8523500);
    $data[] = array(8523600);
    $data[] = array(8531500);
    $data[] = array(8531600);

    foreach (
    $data as $item) {
        
    $totali[$item[0]][$item[1]] += $item[2];
    }
    print_r($totali);
    ?>

  5. #5
    l'array però è composto così:

    $array[] = array(''id_room'' => $id_room, ''id_treatment''=> $id_treatment, ''price''=> $price);

    Devo sempre sommare i vari array che hanno id room e trattamento uguali.

    In pratica ogni array è il prezzo della stanza in un certo trattamento per un periodo del soggiorno. Quindi devo sommare tutti i periodi spezzettati e dare il prezzo finale per ogni trattamento e per ogni camera.

  6. #6
    Qual e' il problema? E' la stessa cosa, soltanto usando gli indici alfabetici invece di quelli numerici:

    Codice PHP:
    <?php

    $data
    [] = array('id_room' => 852'id_treatment' => 1'price' => 400);
    $data[] = array('id_room' => 852'id_treatment' => 1'price' => 250);
    $data[] = array('id_room' => 852'id_treatment' => 2'price' => 300);
    $data[] = array('id_room' => 852'id_treatment' => 2'price' => 500);
    $data[] = array('id_room' => 852'id_treatment' => 3'price' => 500);
    $data[] = array('id_room' => 852'id_treatment' => 3'price' => 600);
    $data[] = array('id_room' => 853'id_treatment' => 1'price' => 500);
    $data[] = array('id_room' => 853'id_treatment' => 1'price' => 600);

    foreach (
    $data as $item) {
        
    $totali[$item['id_room']][$item['id_treatment']] += $item['price'];
    }
    print_r($totali);
    ?>

  7. #7
    In un'altro forum mi hanno consigliato questo

    Codice PHP:
    $somme = array();
    $flag_trovato false;

    foreach ( 
    $arr as $recordA ) {
        foreach ( 
    $somme as $recordSomme ) {
            if ( 
    $recordA['ID ROOM'] === $recordSomme['ID ROOM'] && $recordA['ID TRATTAMENTO'] === $recordSomme['ID TRATTAMENTO'] ) {
                
    $recordSomme['PREZZO'] += $recordA['PREZZO'];
                
    $flag_trovato true;
                break;
            }
        }
        if ( !
    $flag_trovato ) {
            
    $sommme[] = $recordA;
        }
        
    $flag_trovato false;


  8. #8
    Cosa non andava nella mia soluzione?

  9. #9
    Allora, ti stampo il var_dump dell'array

    array(9) {
    [0]=> array(3) { ["room"]=> string(3) "842" ["treatment_type"]=> string(1) "1" ["price"]=> int(420) }
    [1]=> array(3) { ["room"]=> string(3) "842" ["treatment_type"]=> string(1) "2" ["price"]=> int(560) }
    [2]=> array(3) { ["room"]=> string(3) "843" ["treatment_type"]=> string(1) "2" ["price"]=> int(630) }
    [3]=> array(3) { ["room"]=> string(3) "842" ["treatment_type"]=> string(1) "1" ["price"]=> int(70) }
    [4]=> array(3) { ["room"]=> string(3) "842" ["treatment_type"]=> string(1) "2" ["price"]=> int(90) }
    [5]=> array(3) { ["room"]=> string(3) "843" ["treatment_type"]=> string(1) "2" ["price"]=> int(100) }
    [6]=> array(3) { ["room"]=> string(3) "843" ["treatment_type"]=> string(1) "2" ["price"]=> int(100) }
    [7]=> array(3) { ["room"]=> string(3) "842" ["treatment_type"]=> string(1) "1" ["price"]=> int(70) }
    [8]=> array(3) { ["room"]=> string(3) "842" ["treatment_type"]=> string(1) "2" ["price"]=> int(90) }
    }

    e mi stampa

    Array (
    [842] =>
    Array ( [1] => 560
    [2] => 740 )

    [843] => Array (
    [2] => 830 )
    )

  10. #10

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.