我有两个数组
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
)
)
我想将fMaxtTemp
的{{1}} amd fMinTemp
切换为Array2
的{{1}},仅用于Array2的第一个元素。所以
Array1
将成为
[2] => Array (
[fStartValidTime] => 2012-03-13T16:00:00-7:00
[fMaxTemp] => 6
[fMinTemp] => 6
[fWeatherType] => chancerain
[fProbability] => likely
[fCloudCoverPercent] => 20
)
答案 0 :(得分:1)
这是一个简单的解决方案:
新版本回应DaveRandom的评论:
<?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'];
}
原始版本:
<?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'];
}
或者,如果你想得到想象并在你想要的任意数量的阵列位置替换尽可能多的键:
<?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>";
答案 1 :(得分:1)
// 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'];