Reputation: 28334
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
)
Upvotes: 0
Views: 99
Reputation: 448
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>";
Upvotes: 1
Reputation: 88707
// Make sure the array pointers are at the beginning
reset($arr1);
reset($arr2);
// Get the the key of the first element from each array
$k1 = key($arr1);
$k2 = key($arr2);
// Update the values in $arr1 with those from $arr2
$arr2[$k2]['fMaxtTemp'] = $arr1[$k1]['fMaxtTemp'];
$arr2[$k2]['fMintTemp'] = $arr1[$k1]['fMintTemp'];
Upvotes: 1