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

  • SEARCH
  • Home
  • 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 7644029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:35:21+00:00 2026-05-31T09:35:21+00:00

I have two arrays ARRAY 1: Array ( [0] => Array ( [fStartValidTime] =>

  • 0

I have two arrays

ARRAY 1:

Array (
  [0] => Array (
    [fStartValidTime] => 2012-03-13
    [fMaxTemp] => 7
    [fMinTemp] => 2
  )
  [1] => Array (
    [fStartValidTime] => 2012-03-14
    [fMaxTemp] => 7
    [fMinTemp] => 5
  )
  [2] => Array (
    [fStartValidTime] => 2012-03-15
    [fMaxTemp] => 10
    [fMinTemp] => 5
  )
  [3] => Array (
    [fStartValidTime] => 2012-03-16
    [fMaxTemp] => 10
    [fMinTemp] => 1
  )
  [4] => Array (
    [fStartValidTime] => 2012-03-17
    [fMaxTemp] => 7
    [fMinTemp] => 3
  )
  [5] => Array (
    [fStartValidTime] => 2012-03-18
    [fMaxTemp] => 7
    [fMinTemp] => 3
  )
  [6] => Array (
    [fStartValidTime] => 2012-03-19
    [fMaxTemp] => 7
    [fMinTemp] => 1
  )
)

ARRAY 2:

Array (
  [2] => Array (
    [fStartValidTime] => 2012-03-13T16:00:00-7:00
    [fMaxTemp] => 6
    [fMinTemp] => 6
    [fWeatherType] => chancerain
    [fProbability] => likely
    [fCloudCoverPercent] => 20
  )
  [5] => Array (
    [fStartValidTime] => 2012-03-13T19:00:00-7:00
    [fMaxTemp] => 6
    [fMinTemp] => 6
    [fWeatherType] => chancerain
    [fProbability] => likely
    [fCloudCoverPercent] => 20
  )
  [8] => Array (
    [fStartValidTime] => 2012-03-13T22:00:00-7:00
    [fMaxTemp] => 5
    [fMinTemp] => 5
    [fWeatherType] => chancerain
    [fProbability] => likely
    [fCloudCoverPercent] => 20
  )
)

I want to switch the fMaxtTemp amd fMinTemp of Array2 to that of Array1 for just the first element of Array2. So

  [2] => Array (
    [fStartValidTime] => 2012-03-13T16:00:00-7:00
    [fMaxTemp] => 6
    [fMinTemp] => 6
    [fWeatherType] => chancerain
    [fProbability] => likely
    [fCloudCoverPercent] => 20
  )

will become

  [2] => Array (
    [fStartValidTime] => 2012-03-13T16:00:00-7:00
    [fMaxTemp] => 7
    [fMinTemp] => 2
    [fWeatherType] => chancerain
    [fProbability] => likely
    [fCloudCoverPercent] => 20
  )
  • 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-31T09:35:22+00:00Added an answer on May 31, 2026 at 9:35 am

    Here is a simple solution:

    NEW VERSION in response to DaveRandom’s comment:

    <?php
    $tmp = array_keys($array2);
    $array2_first_idx = $tmp[0];
    if ( count($array1) > 0 && count($array2) > 0 ) { // Prevent PHP Notice for empty arrays
        $array2[$array2_first_idx]['fMaxTemp'] = $array1[0]['fMaxTemp'];
        $array2[$array2_first_idx]['fMinTemp'] = $array1[0]['fMinTemp'];
    }
    

    ORIGINAL VERSION:

    <?php
    if ( count($array1) > 0 && count($array2) > 0 ) { // Prevent PHP Notice for empty arrays
        $array2['0']['fMaxTemp'] = $array1['0']['fMaxTemp'];
        $array2['0']['fMinTemp'] = $array1['0']['fMinTemp'];
    }
    

    Or, if you want to get fancy and replace as many keys in as many array positions as you want:

    <?php
    public function swap_array_values($a1, $a2, $pos, $keys)
    {
        $tmp = $a2;
    
        foreach ($pos AS $idx) {
            // Make sure we have a value in the position we want to swap
            if ( array_key_exists($idx, $a1) && array_key_exists($idx, $a2) ) {
                foreach ($keys AS $key) {
                    if ( array_key_exists($key, $a1[$idx]) && array_key_exists($key, $a2[$idx]) ) {
                        $tmp[$idx][$key] = $a1[$idx][$key];
                    }
                }
            }
        }
    
        return $tmp;
    }
    
    // -------------------------------------------------------------------
    
    $positions = array('0');
    $swap_keys = array('fMaxTemp', 'fMinTemp');
    
    echo "Before:<br/>";
    echo "<strong>Array 1</strong><pre>"; print_r($array1); echo "</pre>";
    echo "<strong>Array 2</strong><pre>"; print_r($array2); echo "</pre>";
    
    $array2 = swap_array_values($array1, $array2, $positions, $swap_keys);
    
    echo "<hr/>After:<br/>";
    echo "<strong>Array 2</strong><pre>"; print_r($array2); echo "</pre>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays: $array1 = array(1=>1,10=>1,12=>0,13=>13); $array2 = array(1=>Hello,10=>Test,12=>check,13=>error); Here $array1 has keys
Consider I have two arrays: $friends = Array('foo', 'bar', 'alpha'); $attendees = Array('foo', 'bar');
i have two arrays like this first array Array ( [0228] => Array (
I have two arrays the first one has this structure Parameter Keys array array
Assume that I have two arrays as follow: $array1 = array(1, 3, 5); $array2
Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
I have two different arrays. One array, a, for a list of people. My
I have the following three arrays and need to create a new two-dimensional array
I have two arrays: Array ( [0] => 2 [1] => 3 ) and
I have two arrays each array has some values for instance: int a[] =

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.