php學(xué)習(xí) 數(shù)組課件第1/2頁
Jun 13, 2016 pm 12:28 PM
下標(biāo):數(shù)組中的識(shí)別名稱?也就是字符串或整數(shù)在數(shù)組中的代號(hào)
數(shù)組中有幾個(gè)索引值就被稱為幾維數(shù)組。
索引值:索引是對數(shù)據(jù)庫表中一列或多列的值進(jìn)行排序的一種結(jié)構(gòu)。
????數(shù)組分類
在PHP數(shù)組被分為兩種:
索引數(shù)組:索引(indexed)索引值是整數(shù),以0開始,當(dāng)通過位置來標(biāo)識(shí)東西時(shí)用索引數(shù)組。
關(guān)聯(lián)數(shù)組:關(guān)聯(lián)(associative)關(guān)聯(lián)以字符串做索引值,索引值為列名,用語訪問列的數(shù)據(jù)。
????數(shù)組通常用賦值的方式
一般情況下數(shù)組賦值有兩種方式:
$a[1]="dsadsadsa";
$b[2]="dsadsadsad";
使用array函數(shù):
$a=array("dsads","dsadsa",321312);
一維數(shù)組:數(shù)組的索引值(下標(biāo))只有一個(gè)的時(shí)候稱之為一維數(shù)組。
數(shù)組直接賦值的格式:
$數(shù)組變量名[索引值]=資料內(nèi)容;
注意:索引值可以是字符串也可以是整數(shù)??但是1與“1”是不同的?它們一個(gè)屬于整數(shù)一個(gè)屬于字符串。
同名沒有給予索引值的數(shù)組是按照順序排列的。
實(shí)例:
??????$a=array(1,2,3,4,5,6);
????$b=array("one",?"two",?"three");
????$c=array(0=>"aaa",1=>"bbb",2=>"ccc");
????$d=array("aaa",6=>"bbb","ccc");
????$e=array("name"=>"zhang",?"age"=>20);
???>
????二維數(shù)組
多維數(shù)組的格式:
$a[0][]="dsadas";
$a[0][]="dsadsa";??這組是$a下的0索引值下的1和2
如果用array函數(shù)聲明格式如下:
$a=array("dsadsa","dsadas",21,array("dsadsa","dsadas"));
????數(shù)組的遍歷
foreach循環(huán)結(jié)構(gòu):
foreach?僅用與數(shù)組的循環(huán)?兩種格式
foreach(array_exprssion(數(shù)組表達(dá))?as?$value);
foreach(array_exprssion(數(shù)組表達(dá))?as?$key=>$value);
第一種格式遍歷給定了array_exprssion數(shù)組。每一次循環(huán)中當(dāng)前的值都被賦給我$calue,并且數(shù)組內(nèi)部的指針向前移動(dòng)一步。
第二種格式做同樣的事,只除了當(dāng)前單元的鍵值也會(huì)在每次循環(huán)中被賦給變量?$key。?
當(dāng)?foreach?開始執(zhí)行時(shí),數(shù)組內(nèi)部的指針會(huì)自動(dòng)指向第一個(gè)單元。此外注意foreach?所操作的是指定數(shù)組的一個(gè)拷貝,而不是該數(shù)組本身
??$arr=array(10,20,30,40,50,60);
??foreach($arr?as?$k=>$v){
????echo?"$k=>$v
";
??}
輸出結(jié)果:0=>10?1=>20?2=>30?3=>40?4=>50?5=>60//下標(biāo)=>整數(shù)
????聯(lián)合使用list(),each()和while循環(huán)
each():
??$arr=array(1,2,3,4,5);
??$a=each($arr);??
??print_r($a);
輸出結(jié)果:Array?(?[1]?=>?1?[value]?=>?1?[0]?=>?0?[key]?=>?0?)?
拿取數(shù)組第一位的值value?下標(biāo)key
list():
??$arr3=array("a","b","c");????
??list($key,$value)=each($arr3);
??echo?$key."
".$value;
輸出結(jié)果:0?a?????list()可以說是用一步操作給一組變量賦值?僅能用于數(shù)字索引的數(shù)組并假定數(shù)字索引從?0?開始。
while循環(huán)
??$arr=array(1,2,3,4,5,6,7,8,9,);
??while(list($key,$value)=each($arr)){
??????$key++;
??????echo?$key."=>".$value;
??????echo?"
";
??}
??echo?"
";
輸出結(jié)果:1=>1?2=>2?3=>3?4=>4?5=>5?6=>6?7=>7?8=>8?9=>9
reset()數(shù)組指針重定向
在執(zhí)行?each()?之后,數(shù)組指針將停留在數(shù)組中的下一個(gè)單元或者當(dāng)碰到數(shù)組結(jié)尾時(shí)停留在最后一個(gè)單元。
is_array檢測變量是否為數(shù)組真返回ture假false
??$arr=array(1,2,3,4,5,6,"saas");
??while(list($k,$v)?=?each($arr))
??{
????if(is_array($arr))
????{
??????$x?+=?$v;
??????echo?$x;
????}
????else
????{
??????$x?+=?$k;
????}
??}
這個(gè)范例不能完全體現(xiàn)is_array的功能,但是可以參考。
????數(shù)組的指針
next():負(fù)責(zé)將指針向后移動(dòng)
prve():負(fù)責(zé)將指針向前移動(dòng)
end():會(huì)將指針指向數(shù)組最后一個(gè)元素
reset():將目前指針無條件移至第一個(gè)索引位置
語法格式:mixed?next(數(shù)組名稱)
??$arr=(array(1,2,3,4,5));
??echo?end($arr);
輸出結(jié)果:5
????key()與current()和count()
key()的函數(shù)是讀取目前指針?biāo)赶虻馁Y料的索引值。
current()的函數(shù)則是讀取目前指針?biāo)赶蛸Y料的內(nèi)容資料。
count()的函數(shù)是用來計(jì)算數(shù)組中所有元素的個(gè)數(shù),也就是說函數(shù)會(huì)傳回目標(biāo)數(shù)組的長度值。
格式:int?count(數(shù)組名稱);
key():從關(guān)聯(lián)數(shù)組中取得鍵名
$array?=?array('fruit1'?=>?'apple','fruit2'?=>?'orange','fruit3'?=>?'grape','fruit4'?=>?'apple','fruit5'?=>?'apple');
??while?($fruit_name?=?current($array))?{
??????if?($fruit_name?==?'apple')?{
????????echo?key($array).'
';
??????}
??????next($array);
??}
輸出結(jié)果:fruit1,fruit4,fruit5
current():返回?cái)?shù)組中的當(dāng)前單元
??$transport?=?array('foot',?'bike',?'car',?'plane');
??$mode?=?current($transport);?//?$mode?=?'foot';
??$mode?=?next($transport);????//?$mode?=?'bike';
??$mode?=?current($transport);?//?$mode?=?'bike';
??$mode?=?prev($transport);????//?$mode?=?'foot';
??$mode?=?end($transport);?????//?$mode?=?'plane';
??$mode?=?current($transport);?//?$mode?=?'plane';
注意看范例?返回?cái)?shù)組中的當(dāng)前單元
count():計(jì)算數(shù)組中單元的個(gè)數(shù)
??$arr=array(1,2,3,4,5,6);
??echo?count($arr);
輸出結(jié)果:6
????array_change_key_case()
array_change_key_case返回字符串鍵名全為小寫或者大寫的數(shù)組
其中包含的形態(tài)函數(shù)有兩個(gè)[CASE_UPPER]轉(zhuǎn)換為大寫,[CAS_LOWER]轉(zhuǎn)換為小寫。
??$input_array?=?array("FirSt"?=>?1,?"SecOnd"?=>?4);
??print_r(array_change_key_case($input_array,?CASE_UPPER));
輸出結(jié)果:Array?(?[FIRST]?=>?1?[SECOND]?=>?4?)?
????array_chunk()
array_chunk()次函數(shù)會(huì)將目標(biāo)數(shù)組的資料內(nèi)容,以指定索引個(gè)數(shù),分解成數(shù)個(gè)小型數(shù)組包含在原數(shù)組中。
??$arr=array(1,2,3,4,5,6);
??$a=array_chunk($arr,3);
??print_r($a);
輸出結(jié)果:Array?(?[0]?=>?Array?(?[0]?=>?1?[1]?=>?2?[2]?=>?3?)?[1]?=>?Array?(?[0]?=>?4?[1]?=>?5?[2]?=>?6?)?)
也就是等于用數(shù)組單元個(gè)數(shù)的總和除以3
????array_count_values
array_count_values??用來計(jì)算目標(biāo)數(shù)組中各值出現(xiàn)的次數(shù)
語法格式:array_count_values(目標(biāo)數(shù)組)
此函數(shù)所傳回結(jié)果值,會(huì)以原數(shù)組的內(nèi)容資料作為索引,以數(shù)組的形態(tài)表現(xiàn)。
??$arr=array(1,2,3,3,2,6);
??print_r(array_count_values($arr));
輸出結(jié)果:Array?(?[1]?=>?1?[2]?=>?2?[3]?=>?2?[6]?=>?1?)??
????

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

The core role of Homebrew in the construction of Mac environment is to simplify software installation and management. 1. Homebrew automatically handles dependencies and encapsulates complex compilation and installation processes into simple commands; 2. Provides a unified software package ecosystem to ensure the standardization of software installation location and configuration; 3. Integrates service management functions, and can easily start and stop services through brewservices; 4. Convenient software upgrade and maintenance, and improves system security and functionality.
