我有两个数组
第一个数组
array(
[0] => +970
[1] => +971
[2] => +972
)
和第二个数组
array(
[0] => 465465454
[1] => 321321355
[2] => 987946546
)
我想像这样合并它们
array(
[+970] => 465465454
[+971] => 321321355
[+972] => 987946546
)
我尝试array_merge
,但这给了我一些我不想要的结果。
$busi_code = $page1_data->business_code; //array
$busi_num = $page1_data->business_number; //array
$business_phone_numbers = array_merge($busi_code, $busi_num);
echo '<pre>';
print_r($business_phone_numbers);
echo '</pre>';
结果是
[0] => +970
[1] => +971
[2] => +972
[3] => 465465454
[4] => 321321355
[5] => 987946546
所以请指导我如何达到我要求的结果。
答案 0 :(得分:6)
您正在寻找array_combine
,而不是array_merge
:
使用keys数组中的值作为键,将values数组中的值作为相应的值,创建一个数组。
$business_phone_numbers = array_combine($busi_code, $busi_num);
答案 1 :(得分:4)
这是$business_phone_numbers = array_combine($busi_code, $busi_num);
功能的工作:
function groupByInterval(data, secondsPerGroup) {
// First sort by the time stamps in ascending order
data = data.sort( ([a], [b]) => a - b )
// Then add the starting time stamps of the groups
.map(([unix, price]) => ({ unixstart: unix - unix % secondsPerGroup, price }) );
// Create an entry per group; initialise values as empty arrays (for prices)
const map = new Map(data.map( ({unixstart}) => [unixstart, []] ));
return Array.from(
// Collect the prices per group
data.reduce( (acc, {unixstart, price}) =>
acc.set(unixstart, acc.get(unixstart).concat(price)),
map
).entries(),
// Enrich the group values with formatted dates and price measures
([unixstart, prices]) => ({
unixstart,
start: new Date(unixstart * 1000).toJSON(),
end: new Date((unixstart + secondsPerGroup) * 1000).toJSON(),
minPrice: Math.min(...prices),
maxPrice: Math.max(...prices),
firstPrice: prices[0],
lastPrice: prices[prices.length-1],
countPrices: prices.length,
sumPrices: prices.reduce( (a,b) => a+b, 0 )
})
);
}
// Sample input
const data = [[1518371928, 15440000],[1518371928, 15440000],[1518371928, 15440000],[1518371928, 15440000],[1518371928, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518294600, 15440000],[1518417005, 15430000],[1518417005, 15430000],[1518420449, 15465000],[1518422229, 15510000],[1518423849, 15535000],[1518425806, 15598000],[1518427146, 15615000],[1518428229, 15625000],[1518430648, 15635000]];
const result = groupByInterval(data, 60*60); // Group per hour (expressed in seconds)
console.log(result);
答案 2 :(得分:-1)
您必须使用array_combine。
试试这个:
$a = array(
0 => +970,
1 => +971,
2 => +972);
$b = array(
0 => 465465454,
1 => 321321355,
2 => 987946546);
$r = array_combine($a,$b);
print_r($r);