php數(shù)組總結(jié)篇(一)
數(shù)組
1.數(shù)組的下標(biāo)是整型數(shù)值或者是字符串類型。
eg1.索引數(shù)組的鍵是______,關(guān)聯(lián)數(shù)組的鍵是______。
2.字符串作為索引的時(shí)候,應(yīng)加上引號(hào)。常量或者變量不用加引號(hào),否則無法編譯。
在php中,沒有引號(hào)的字符串會(huì)自動(dòng)生成一個(gè)裸字符串,而 PHP 可能會(huì)在以后定義此常量,不幸的是你的代碼中有同樣的名字,那么這個(gè)字符串就被重新賦值。
eg2.<?php
// 顯示所有錯(cuò)誤
error_reporting(E_ALL);
$arr = array('fruit' => 'apple', 'veggie' => 'carrot');
// 正確
print $arr['fruit']; // apple
print $arr['veggie']; // carrot
// 不正確。This works but also throws a PHP error of
// level E_NOTICE because of an undefined constant named fruit
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit]; // apple
// Let's define a constant to demonstrate what's going on. We
// will assign value 'veggie' to a constant named fruit.
define('fruit','veggie');
// Notice the difference now
print $arr['fruit']; // apple
print $arr[fruit]; // carrot
// The following is okay as it's inside a string. Constants are not
// looked for within strings so no E_NOTICE error here
print "Hello $arr[fruit]"; // Hello apple
// With one exception, braces surrounding arrays within strings
// allows constants to be looked for
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple
// This will not work, results in a parse error such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using autoglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";
// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?>
3.鍵值問題
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a[] = 4; // key will be 0
$b[] = 'a'; // key will be 0
$b[] = 'b'; // key will be 1
$b[] = 'c'; // key will be 2
switching = array( 10, // key = 0
5 => 6,
3 => 7,
'a' => 4,
11, // key = 6 (maximum of integer-indices was 5)
'8' => 2, // key = 8 (integer!)
'02' => 77, // key = '02'
0 => 12 // the value 10 will be overwritten by 12
);
<?php
$multi_array = array("red",
"green",
42 => "blue","yellow" => array("apple",9 => "pear","banana","orange" => array("dog","cat","iguana")));
?>
A.$multi_array['yellow']['apple'][0]
B.$multi_array['blue'][0]['orange'][1]
C.$multi_array[3][3][2]
D.$multi_array['yellow']['orange']['cat']
E.$multi_array['yellow']['orange'][1]
--------------------------------待續(xù)待續(xù)待續(xù)------
4.array_walk
5.var_dump
6.array_intersect
7.array_sum
8.array_count_values
9.array_flip
10.natsort
11.ksort(),asort(),krsort(),sort(),usort()
12.array_reverse()
13.array_merge
14.reset
-------------------------------待續(xù)待續(xù)待續(xù)------
15.array_combine
16array_count_values
17.array_diff
18.array_filter
19.array_search
相關(guān)文章
php cookie中點(diǎn)號(hào)(句號(hào))自動(dòng)轉(zhuǎn)為下劃線問題
這篇文章主要介紹了php cookie中點(diǎn)號(hào)(句號(hào))自動(dòng)轉(zhuǎn)為下劃線問題,需要的朋友可以參考下2014-10-10
取得單條網(wǎng)站評(píng)論以數(shù)組形式進(jìn)行輸出
這篇文章主要介紹了取得單條網(wǎng)站評(píng)論方法并以數(shù)組形式進(jìn)行輸出,需要的朋友可以參考下2014-07-07
PHP實(shí)現(xiàn)將科學(xué)計(jì)數(shù)法轉(zhuǎn)換為原始數(shù)字字符串的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)將科學(xué)計(jì)數(shù)法轉(zhuǎn)換為原始數(shù)字字符串的方法,通過一個(gè)簡(jiǎn)單的自定義函數(shù)使用正則替換實(shí)現(xiàn)該功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12
PHP mongodb操作類定義與用法示例【適合mongodb2.x和mongodb3.x】
這篇文章主要介紹了PHP mongodb操作類定義與用法,結(jié)合實(shí)例形式分析了php封裝的適合mongodb2.x和mongodb3.x版本MongoDB數(shù)據(jù)庫(kù)連接、增刪改查、錯(cuò)誤處理等操作定義與使用方法,需要的朋友可以參考下2018-06-06
PHP設(shè)計(jì)模式之簡(jiǎn)單投訴頁(yè)面實(shí)例
這篇文章主要為大家詳細(xì)介紹了PHP設(shè)計(jì)模式下簡(jiǎn)單投訴頁(yè)面實(shí)例,感興趣的小伙伴們可以參考一下2016-02-02
PHP門面模式實(shí)現(xiàn)簡(jiǎn)單的郵件發(fā)送示例
這篇文章主要為大家介紹了PHP門面模式實(shí)現(xiàn)簡(jiǎn)單的郵件發(fā)送示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

