Home Backend Development PHP Tutorial php 归拢两个有序数组

php 归拢两个有序数组

Jun 13, 2016 pm 01:20 PM
amp arr count lt

php 合并两个有序数组

对于两个有序数组,每个数组都是循环一次,即可有序的排列到新的数组里面;

采取主意递增,比较,然后按顺序插入,php代码:

<?php $arr1 = array(1,2,3,4,5,6,7,8);//示例数据
$arr2 = array(3,4,5,7,9,10);
echo '<pre class="brush:php;toolbar:false">';
print_r(mergeOrderly($arr1,$arr2));//示例
function mergeOrderly($arr1,$arr2){
	if(!count($arr1)){//判断参数是否有意义
		return false;
	}elseif(!count($arr2)){
		return false;
	}else {//进行合并
		$arr = array();//定义最终数组容器
		$arr2Num = count($arr2);//统计数组长度
		$arr1Num = count($arr1);
		$i1 = 0;//数组1 的循环标记
		$i2 = 0;//数组2 的循环标记
		while($i1  $arr2[$i2]){//需要比较数组1和数组2,小的放入目标数组,并且标记+1
					$arr[] = $arr2[$i2];
					$i2++;
				}else{
					$arr[] = $arr1[$i1];
					$i1++;
				}
			}elseif($i1 = $arr2Num){//数组2 已经到达末尾,而数组1还为到达,情况二
					$arr[] = $arr1[$i1];//直接把数组1的内容插入到目标数组中
					$i1++;
			}elseif($i2 = $arr1Num){//数组1已经到达末尾,而数组2还未到达,情况三
					$arr[] = $arr2[$i2];//直接把数组2的内容插入到目标数组中
					$i2++;
			}
		}
		return $arr;
	}
}
?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between counta and count The difference between counta and count Nov 20, 2023 am 10:01 AM

The Count function is used to count the number of numbers in a specified range. It ignores text, logical values, and null values, but counts empty cells. The Count function only counts the number of cells that contain actual numbers. The CountA function is used to count the number of non-empty cells in a specified range. It not only counts cells containing actual numbers, but also counts the number of non-empty cells containing text, logical values, and formulas.

What coin is AMP? What coin is AMP? Feb 24, 2024 pm 09:16 PM

What is AMP Coin? The AMP token was created by the Synereo team in 2015 as the main trading currency of the Synereo platform. AMP token aims to provide users with a better digital economic experience through multiple functions and uses. Purpose of AMP Token The AMP Token has multiple roles and functions in the Synereo platform. First, as part of the platform’s cryptocurrency reward system, users are able to earn AMP rewards by sharing and promoting content, a mechanism that encourages users to participate more actively in the platform’s activities. AMP tokens can also be used to promote and distribute content on the Synereo platform. Users can increase the visibility of their content on the platform by using AMP tokens to attract more viewers to view and share

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Summary of usage differences of MySQL Count function Summary of usage differences of MySQL Count function Nov 29, 2022 pm 04:47 PM

This article introduces to you the usage differences of MySQL’s Count function. The need for statistical data is very easy to encounter in our daily development. MySQL also supports a variety of calculation functions. Next, let’s take a look at the differences between them. , and whether they have some pitfalls.

What is the usage of count function in php What is the usage of count function in php Sep 09, 2021 pm 05:08 PM

The usage of the count function in PHP is: [count(array,mode);], where the parameter array specifies the array to be counted, and the parameter mode specifies the mode of the function.

Microsoft is rolling out Windows 11 23H2 build to the release preview channel with Copilot Microsoft is rolling out Windows 11 23H2 build to the release preview channel with Copilot Sep 28, 2023 pm 07:17 PM

Everyone is looking forward to today's Windows 1123H2 release. In fact, Microsoft has just launched updates to the release preview, which is the closest channel before the official release stage. Known as Build 22631, Microsoft said they are rolling out the new rebranded chat app, phone link, and play together widgets that have been tested on other internal channels over the past few months. "This new update will have the same servicing branch and codebase as Windows 11 version 22H2 and will be cumulative with all newly announced features, including Copilot in Windows (preview)," Microsoft promises. Redmond officials further

How to solve the problem of count distinct multiple columns in mysql How to solve the problem of count distinct multiple columns in mysql Jun 03, 2023 am 10:49 AM

The reproduced test database is as follows: CREATETABLE`test_distinct`(`id`int(11)NOTNULLAUTO_INCREMENT,`a`varchar(50)CHARACTERSETutf8DEFAULTNULL,`b`varchar(50)CHARACTERSETutf8DEFAULTNULL,PRIMARYKEY(`id`))ENGINE= InnoDBAUTO_INCREMENT=1DEFAULTCHARSET=latin1;The test data in the table is as follows. Now we need to count the deduplicated columns of these three columns.

How does java Count count elements in a stream? How does java Count count elements in a stream? May 11, 2023 pm 04:07 PM

Note 1. Count is a terminal operation that can count the total number of elements in the stream. The return value is of long type. 2. count() returns the count of elements in the stream. This is a special case of induction (an induction operation takes a sequence of input elements and combines them into a summary result by repeatedly applying the combining operation). This is a terminal operation and may have consequences and side effects. After a terminal operation is performed, the pipe is considered consumed and cannot be reused. Example // Verify whether there is a string in the list starting with a, and match the first one, that is, return truebooleananyStartsWithA=stringCollection.stream().anyMatch((s

See all articles