Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6681115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:33:13+00:00 2026-05-26T04:33:13+00:00

I am having some difficulty looping through an array and calculating fields. Here is

  • 0

I am having some difficulty looping through an array and calculating fields. Here is the array $iroom:

Array
(
   [num_rooms] => 2
   [adults] => Array
    (
        [0] => 2
        [1] => 2
    )

   [prices] => Array
    (
        [0] => 44.5
        [1] => 44.5
    )

   [roomTotalPrice] => Array
    (
        [0] => 89
        [1] => 89
    )

   [price] => 178
)

I want to (adults*prices)+(adults*$asup)+(chidern*$csup)+$ssup and pu the answer into the roomTotalPrice. So far the outer forloop sets the roomTotalPrice price but I cannot get the inner loops to calculate the price. The $sup are extra supplement prices.

The code I got so far:

                        foreach($iroom['roomTotalPrice'] as &$irt){
                            foreach($iroom['adults'] as $ira){

                            }
                            $irt = ;
                        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:33:14+00:00Added an answer on May 26, 2026 at 4:33 am

    CODE WRAPPED IN FUNCTION, TO HANDLE NEW ARRAY FORMAT

    /*
      Note that this function may not be 100% correct. I notice you have removed
      the 'supp' key from the array, and that your current spec doesn't do anything
      with the 'price' key. I suspect you may want the line
    
        + ((isset($array['supp'])) ? $array['supp'] : 0);
    
      to read
    
        + ((isset($array['price'])) ? $array['price'] : 0);
    
    */    
    
    function calculateTotalPrices ($array, $asup = 10, $csup = 10) {
      if (!is_array($array) || !isset($array['num_rooms']) || !$array['num_rooms']) return FALSE; // make sure data is valid
      for ($i = 0; $i < $array['num_rooms']; $i++) { // Loop num_rooms times
        $array['roomTotalPrice'][$i] =
          ((isset($array['adults'][$i],$array['prices'][$i])) ? ($array['adults'][$i] * $array['prices'][$i]) + ($array['adults'][$i] * $asup) : 0) // Calculate price for adults
          + ((isset($array['childern'][$i])) ? ($array['childern'][$i] * $csup) : 0) // Calculate price for children
          + ((isset($array['supp'])) ? $array['supp'] : 0); // Add the supplement
      }
      // Get a total price for adults + children + supplements for all rooms
      $array['grandTotal'] = array_sum($array['roomTotalPrice']);
      return $array;
    }
    
    $iroom = array (
      'num_rooms' => 2,
      'adults' => array (
        0 => 2,
        1 => 3
      ),
      'childern' => array (
        0 => 1,
        1 => 2
      ),
      'prices' => array (
        0 => 44.5,
        1 => 44.5
      ),
      'price' => 178,
    );
    
    print_r(calculateTotalPrices($iroom));
    /* With the above array, outputs
    Array
    (
        [num_rooms] => 2
        [adults] => Array
            (
                [0] => 2
                [1] => 3
            )
    
        [childern] => Array
            (
                [0] => 1
                [1] => 2
            )
    
        [prices] => Array
            (
                [0] => 44.5
                [1] => 44.5
            )
    
        [price] => 178
        [roomTotalPrice] => Array
            (
                [0] => 119
                [1] => 183.5
            )
    
        [grandTotal] => 302.5
    )
    */
    
    print_r(calculateTotalPrices($iroom,20,25));
    /* With your sample array, outputs
    Array
    (
        [num_rooms] => 2
        [adults] => Array
            (
                [0] => 2
                [1] => 3
            )
    
        [childern] => Array
            (
                [0] => 1
                [1] => 2
            )
    
        [prices] => Array
            (
                [0] => 44.5
                [1] => 44.5
            )
    
        [price] => 178
        [roomTotalPrice] => Array
            (
                [0] => 154
                [1] => 243.5
            )
    
        [grandTotal] => 397.5
    )
    */      
    

    CODE UPDATED WITH ADDITIONAL CHECKS

    foreach ($iroom as $k1 => $v1) { // Loop outer array
      if (is_array($v1)) { // Make sure element is an array
        foreach ($v1 as $k2 => $v2) { // Loop inner array
          if (is_array($v2)) { // Make sure element is an array
            for ($i = 0; $i < $v2['num_rooms']; $i++) { // Loop num_rooms times
              $iroom[$k1][$k2]['roomTotalPrice'][$i] =
                ((isset($v2['adults'][$i],$v2['prices'][$i])) ? ($v2['adults'][$i] * $v2['prices'][$i]) + ($v2['adults'][$i] * $asup) : 0) // Calculate price for adults
                + ((isset($v2['childern'][$i])) ? ($v2['childern'][$i] * $csup) : 0) // Calculate price for children
                + $v2['supp']; // Add the supplement
            }
            // Get a total price for adults + children + supplements for all rooms
            $iroom[$k1][$k2]['grandTotal'] = array_sum($iroom[$k1][$k2]['roomTotalPrice']);
          }
        }
      }
    }
    
    print_r($iroom);
    

    EDIT

    Using the exact code above, feeding in the array above, and setting $asup = $csup = 10; at the top, I get no errors, and this output:

    Array
    (
        [10] => Array
            (
                [12] => Array
                    (
                        [num_rooms] => 2
                        [adults] => Array
                            (
                                [0] => 2
                                [1] => 3
                            )
    
                        [childern] => Array
                            (
                                [0] => 1
                                [1] => 2
                            )
    
                        [prices] => Array
                            (
                                [0] => 44.5
                                [1] => 44.5
                            )
    
                        [price] => 178
                        [supp] => 0
                        [roomTotalPrice] => Array
                            (
                                [0] => 119
                                [1] => 183.5
                            )
    
                        [grandTotal] => 302.5
                    )
    
            )
    
    )
    

    Note that the first result comes out at 119, not 129 as you state in the comment above – this is because in your example array, supp is 0 and not 10, as you have used in your calculation.

    I have also tested with more complex arrays (with more elements at the first and second levels) and it works fine.

    I’m guessing if you are getting “invalid argument supplied for foreach” errors it’s because in your actual array, you highest level has some non-array memebers. This can easily be overcome by changing

    foreach ($v1 as $k2 => $v2) {
    

    to

    if (is_array($v1)) foreach ($v1 as $k2 => $v2) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some difficulty writing a function that will search through a directory
im having some difficulty trying to pull out a specific value from a cookie.
I am having some difficulty accessing the value of the selected radio button in
I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that
I'm having some difficulty with my Entity Framework context that is proving very troublesome
I've been having some difficulty in understanding the source of a problem. Below is
I'm having some difficulty figuring out the best ways to pause and resume my
I'm having some difficulty figuring out what is going on and how to fix
I am having some difficulty creating a bundle for my application, and placing files
I am writing a GAE application and am having some difficulty with the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.