導航:首頁 > 編程語言 > php數組下標從1開始

php數組下標從1開始

發布時間:2022-09-26 01:39:44

php中多維數組的問題

"Griffin"=>array()
表示索引"Griffin"是一個數組。=>可以簡單理解為賦值。這是php里特有的一種寫法。
數組分為2種,一種是自動索引數組。比如
$x=array ("Peter","Lois", "Megan");
那麼$X[0]值為"peter",$X[1]為lois。
還有一種是自定義索引數組。
比如
$x=array ("father"=>"Peter","mother"=>"Lois","son"=> "Megan");
那麼$x["father"] 就為"peter"
用引號圍起來表示這是一個索引字元串值。通常情況下你直接[Griffin]也可以。
但是如果你在系統里有一個變數
$Griffin="son";
那麼$families[Griffin]實際上會等於$families['son']。所以最好用引號圍起來。
更多詳細可以看php手冊數組一章。

==========================================================
數組
PHP 中的 數組 實際上是一個有序映射。映射是一種把 values 關聯到 keys 的類型。此類型在很多方面做了優化,因此可以把它當成真正的數組,或列表(向量),散列表(是映射的一種實現),字典,集合,棧,隊列以及更多可能性。數組元素的值也可以是另一個數組。樹形結構和多維數組也是允許的。

解釋這些結構超出了本手冊的范圍,但對於每種結構至少會提供一個例子。要得到這些結構的更多信息,建議參考有關此廣闊主題的其它著作。

語法
定義數組 array()
可以用 array() 語言結構來新建一個 array。它接受任意數量用逗號分隔的 鍵(key) => 值(value) 對。

array( key => value
, ...
)
// 鍵(key) 可是是一個 整數(integer) 或 字元串(string)
// 值(value) 可以是任意類型的值<?php
$arr = array("foo" => "bar", 12 => true);

echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
key 可以是 integer 或者 string。如果key是一個 integer 的標准表示,則被解釋為整數(例如 "8" 將被解釋為 8,而 "08" 將被解釋為 "08")。key 中的浮點數被取整為 integer。在 PHP 中索引數組與關聯 數組 是相同的,它們都可以同時包含 整型 和 字元串 的下標。

值可以是任意的 PHP 類型。

<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));

echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>
如果對給出的值沒有指定鍵名,則取當前最大的整數索引值,而新的鍵名將是該值加一。如果指定的鍵名已經有了值,則該值會被覆蓋。

<?php
// 這個數組與下面的數組相同 ...
array(5 => 43, 32, 56, "b" => 12);

// ...
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
Warning
自 PHP 4.3.0 起,上述的索引生成方法改變了。如今如果給一個當前最大鍵名是負值的數組添加一個新值,則新生成的的索引將為零(0)。以前新生成的索引為當前最大索引加一,和正值的索引相同。

使用 TRUE 作為鍵名將使 integer 1 成為鍵名。使用 FALSE 作為鍵名將使 integer 0 成為鍵名。使用 NULL 作為鍵名將等同於使用空字元串。使用空字元串作為鍵名將新建(或覆蓋)一個用空字元串作為鍵名的值,這和用空的方括弧不一樣。

不能用數組和對象作為鍵(key)。這樣做會導致一個警告:Illegal offset type。

用方括弧的語法新建/修改
可以通過明示地設定值來改變一個現有的數組。

這是通過在方括弧內指定鍵名來給數組賦值實現的。也可以省略鍵名,在這種情況下給變數名加上一對空的方括弧(「[]」)。

$arr[key] = value;
$arr[] = value;
// key 可以是 integer 或 string
// value 可以是任意類型的值如果 $arr 還不存在,將會新建一個。這也是一種定義數組的替換方法。要改變一個值,只要給它賦一個新值。如果要刪除一個鍵名/值對,要對它用 unset()。

<?php
$arr = array(5 => 1, 12 => 2);

$arr[] = 56; // This is the same as $arr[13] = 56;
// at this point of the script

$arr["x"] = 42; // This adds a new element to
// the array with key "x"

unset($arr[5]); // This removes the element from the array

unset($arr); // This deletes the whole array
?>
Note:

如上所述,如果給出方括弧但沒有指定鍵名,則取當前最大整數索引值,新的鍵名將是該值 + 1。如果當前還沒有整數索引,則鍵名將為 0。如果指定的鍵名已經有值了,該值將被覆蓋。

注意這里所使用的最大整數鍵名不一定當前就在數組中。它只要在上次數組重新生成索引後曾經存在過就行了。以下面的例子來說明:

<?php
// 創建一個簡單的數組
$array = array(1, 2, 3, 4, 5);
print_r($array);

// 現在刪除其中的所有元素,但保持數組本身不變:
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);

// 添加一個單元(注意新的鍵名是 5,而不是你可能以為的 0)
$array[] = 6;
print_r($array);

// 重新索引:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>
以上常式會輸出:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
)
Array
(
[5] => 6
)
Array
(
[0] => 6
[1] => 7
)

實用函數
有很多操作數組的函數,參見數組函數一節。

Note:

unset() 函數允許刪除數組中的某個鍵。但要注意數組將不會重建索引。 If a true "remove and shift" behavior is desired, the array can be reindexed using the array_values() function.

<?php
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
/* will proce an array that would have been defined as
$a = array(1 => 'one', 3 => 'three');
and NOT
$a = array(1 => 'one', 2 =>'three');
*/

$b = array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
?>

foreach 控制結構是專門用於數組的。它提供了一個簡單的方法來遍歷數組。

數組做什麼和不做什麼
為什麼 $foo[bar] 錯了?
應該始終在用字元串表示的數組索引上加上引號。例如用 $foo['bar'] 而不是 $foo[bar]。但是為什麼 $foo[bar] 錯了呢?可能在老的腳本中見過如下語法:

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>
這樣是錯的,但可以正常運行。那麼為什麼錯了呢?原因是此代碼中有一個未定義的常量(bar)而不是字元串('bar'-注意引號),而 PHP 可能會在以後定義此常量,不幸的是你的代碼中有同樣的名字。它能運行,是因為 PHP 自動將裸字元串(沒有引號的字元串且不對應於任何已知符號)轉換成一個其值為該裸字元串的正常字元串。例如,如果沒有常量定義為 bar,PHP 將把它替代為 'bar' 並使用之。

Note: 這並不意味著總是給鍵名加上引號。用不著給鍵名為常量或變數的加上引號,否則會使 PHP 不能解析它們。

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
// Simple array:
$array = array(1, 2);
$count = count($array);
for ($i = 0; $i < $count; $i++) {
echo "\nChecking $i: \n";
echo "Bad: " . $array['$i'] . "\n";
echo "Good: " . $array[$i] . "\n";
echo "Bad: {$array['$i']}\n";
echo "Good: {$array[$i]}\n";
}
?>
以上常式會輸出:

Checking 0:
Notice: Undefined index: $i in /path/to/script.html on line 9
Bad:
Good: 1
Notice: Undefined index: $i in /path/to/script.html on line 11
Bad:
Good: 1

Checking 1:
Notice: Undefined index: $i in /path/to/script.html on line 9
Bad:
Good: 2
Notice: Undefined index: $i in /path/to/script.html on line 11
Bad:
Good: 2

演示此行為的更多例子:

<?php
// Show all errors
error_reporting(E_ALL);

$arr = array('fruit' => 'apple', 'veggie' => 'carrot');

// Correct
print $arr['fruit']; // apple
print $arr['veggie']; // carrot

// Incorrect. 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

// This defines a constant to demonstrate what's going on. The value 'veggie'
// is assigned 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 occurs here
print "Hello $arr[fruit]"; // Hello apple

// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple

// This will not work, and will result 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 superglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";

// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?>
當打開 error_reporting 來顯示 E_NOTICE 級別的錯誤(例如將其設為 E_ALL)時將看到這些錯誤。默認情況下 error_reporting 被關閉不顯示這些。

和在語法一節中規定的一樣,在方括弧(「[」和「]」)之間必須有一個表達式。這意味著可以這樣寫:

<?php
echo $arr[somefunc($bar)];
?>
這是一個用函數返回值作為數組索引的例子。PHP 也可以用已知常量,可能之前已經見過

<?php
$error_descriptions[E_ERROR] = "A fatal error has occured";
$error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE] = "This is just an informal notice";
?>
注意 E_ERROR 也是個合法的標識符,就和第一個例子中的 bar 一樣。但是上一個例子實際上和如下寫法是一樣的:

<?php
$error_descriptions[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
?>
因為 E_ERROR 等於 1, 等等.

那麼為什麼這樣做不好?
也許有一天,PHP 開發小組可能會想新增一個常量或者關鍵字,或者用戶可能希望以後在自己的程序中引入新的常量,那就有麻煩了。例如已經不能這樣用 empty 和 default 這兩個詞了,因為他們是保留字。

Note: 重申一次,在雙引號字元串中,不給索引加上引號是合法的因此 "$foo[bar]"是合法的(「合法」的原文為valid。在實際測試中,這么做確實可以訪問數組的該元素,但是會報一個常量未定義的notice。無論如何,強烈建議不要使用$foo[bar]這樣的寫法,而要使用$foo['bar']來訪問數組中元素。--haohappy注)。至於為什麼參見以上的例子和字元串中的變數解析中的解釋。

轉換為數組
對於任意類型: integer, float, string, boolean and resource,如果將一個值轉換為數組,將得到一個僅有一個元素的數組(其下標為 0),該元素即為此標量的值。換句話說, (array)$scalarValue 與 array($scalarValue) 完全一樣。

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour:

<?php

class A {
private $A; // This will become '\0A\0A'
}

class B extends A {
private $A; // This will become '\0B\0A'
public $AA; // This will become 'AA'
}

var_mp((array) new B());
?>
The above will appear to have two keys named 'AA', although one of them is actually named '\0A\0A'.

將 NULL 轉換到 數組(array) 會得到一個空的數組。

比較
可能使用 array_diff() 和數組運算符來比較數組。

Examples
PHP 中的數組類型有非常多的用途,因此這里有一些例子展示數組的完整威力。

<?php
// This:
$a = array( 'color' => 'red',
'taste' => 'sweet',
'shape' => 'round',
'name' => 'apple',
4 // key will be 0
);

$b = array('a', 'b', 'c');

// . . .is completely equivalent with this:
$a = array();
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a[] = 4; // key will be 0

$b = array();
$b[] = 'a';
$b[] = 'b';
$b[] = 'c';

// After the above code is executed, $a will be the array
// array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round',
// 'name' => 'apple', 0 => 4), and $b will be the array
// array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c').
?>
Example #1 Using array()

<?php
// Array as (property-)map
$map = array( 'version' => 4,
'OS' => 'Linux',
'lang' => 'english',
'short_tags' => true
);

// strictly numerical keys
$array = array( 7,
8,
0,
156,
-10
);
// this is the same as array(0 => 7, 1 => 8, ...)

$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
);

// empty array
$empty = array();
?>
Example #2 集合

<?php
$colors = array('red', 'blue', 'green', 'yellow');

foreach ($colors as $color) {
echo "Do you like $color?\n";
}

?>
以上常式會輸出:

Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?
直接改變數組的值在 PHP 5 中可以通過引用傳遞來做到。之前的版本需要需要採取變通的方法:

Example #3 集合

<?php
// PHP 5
foreach ($colors as &$color) {
$color = strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */

// Workaround for older versions
foreach ($colors as $key => $color) {
$colors[$key] = strtoupper($color);
}

print_r($colors);
?>
以上常式會輸出:

Array
(
[0] => RED
[1] => BLUE
[2] => GREEN
[3] => YELLOW
)
本例生成一個下標從1開始的數組。This example creates a one-based array.

Example #4 下標從1開始的數組

<?php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>
以上常式會輸出:

Array
(
[1] => 'January'
[2] => 'February'
[3] => 'March'
)
Example #5 填充數組

<?php
// fill an array with all items from a directory
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
closedir($handle);
?>
數組是有序的。也可以使用不同的排序函數來改變順序。更多信息參見數組函數。可以用 count() 函數來數出數組中元素的個數。

Example #6 數組排序

<?php
sort($files);
print_r($files);
?>
因為數組中的值可以為任意值,也可是另一個數組。這樣可以產生遞歸或多維數組。

Example #7 遞歸和多維數組

<?php
$fruits = array ( "fruits" => array ( "a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array ( 1,
2,
3,
4,
5,
6
),
"holes" => array ( "first",
5 => "second",
"third"
)
);

// Some examples to address values in the array above
echo $fruits["holes"][5]; // prints "second"
echo $fruits["fruits"]["a"]; // prints "orange"
unset($fruits["holes"][0]); // remove "first"

// Create a new multi-dimensional array
$juices["apple"]["green"] = "good";
?>
數組(Array) 的賦值總是會涉及到值的拷貝。使用 引用操作符 通過引用來拷貝數組。

<?php
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
// $arr1 is still array(2, 3)

$arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?>

❷ PHP獲取數組下標的問題

PHP 獲取數組指定值的位置或下標代碼如下:

<?php

//定義一個數組

$array = array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');

//使用 array_search('要搜索的值',數組);

$key = array_search('b', $array); // $key = 1;

$key = array_search('a', $array); // $key = 0;

?>

(2)php數組下標從1開始擴展閱讀

php 中的數組實際上是一個有序圖。圖是一種把 values 映射到 keys 的類型。此類型在很多方面做了優化,因此可以把它當成真正的數組來使用,或列表(矢量),散列表(是圖的一種實現),字典,集合,棧,隊列以及更多可能性。

因為可以用另一個 php 數組作為值,也可以很容易地模擬樹。解釋這些結構超出了本手冊的范圍,但對於每種結構至少會發現一個例子。要得到這些結構的更多信息,建議參考有關此廣闊主題的外部著作。

對於任何的類型:整型、浮點、字元串、布爾和資源,如果將一個值轉換為數組,將得到一個僅有一個元素的數組(其下標為 0),該元素即為此標量的值。如果將一個對象轉換成一個數組,所得到的數組的元素為該對象的屬性(成員變數),其鍵名為成員變數名。

如果將一個 NULL 值轉換成數組,將得到一個空數組。

❸ php數組[0],[1],[2]分別是什麼意思

這個是正則表達式中的「反向引用」,用以匹配特定的「局部內容」,
在這里$match[2]是一個數組,數組的內容為"aaa" ,"cccc";

❹ PHP 數組 下標問題

不用循環用什麼?用什麼方法無所謂,只要能讓過程簡單化才是理想結果!
foreach($a as $k=>$v){
if{$k!=='0'}:$k++;
endif;
$b[$k]=$v;
}
print_r($b);
要不然就用數組漫遊,即array_walk,通過函數回調實戰,這樣要啰嗦一些!
還有,,什麼下標啊,,,那是鍵名!

❺ PHP如何取得數組的上標和下標

獲取下標:$array=array('a'=>1,'b'=>3,'c'=>4);$a=array_keys($array);echo end($a)。

PHP的加密函數有crypt()、 md5() 和sha1() 這3種, 其中crypt() 用於單向加密, 所謂的單向加密就是將需要加密的內容進行加密之後, 無法將密文轉換成為可讀的內容。

因此單向加密的應用范圍較狹窄, 一般用於用戶名認證和密碼輸入等情況; 當用戶進入系統時,只需要將密文口令輸 入,經過系統驗證與存儲的口令一致, 即可通過。

(5)php數組下標從1開始擴展閱讀:

主要特點:

(一)開源性和免費性

由於PHP的解釋器的源代碼是公開的,所以安全系數較高的網站可以自己更改PHP的解釋程序。另外,PHP 運行環境的使用也是免費的。

(二)快捷性

PHP是一種非常容易學習和使用的一門語言,它的語法特點類似於C語言,但又沒有C語言復雜的地址操作,而且又加入了面向對象的概念,再加上它具有簡潔的語法規則,使得它操作編輯非常簡單,實用性很強。

(三)資料庫連接的廣泛性

PHP可以與很多主流的資料庫建立起連接,如MySQL、ODBC、Oracle等,PHP是利用編譯的不同函數與這些資料庫建立起連接的,PHPLIB就是常用的為一般事務提供的基庫。

❻ php如何取出下標不是按1遞增的數組的值

1、修改下標從1開始:
<?php

$tmp = array();
$i=1;

foreach($result as $value):
$tmp[$i] = $value;
$i++;
endforeach;
$result = $tmp;//重新賦值,使得$result數組中下標從1開始

?>
2、讀取$result裡面的值:

<?php

//再用一個foreach循環讀取數據行,不懂,可以問我

foreach($result as $value):
print_r($value);
endforeach;

?>
這樣寫就可以讀取$result裡面的值了

我不懂,你說的下標是要按照什麼來輸出。這里說明下函數的作用
<?php
foreach($output['blockData']['floor'] as $floor):
$shuzu[] = $floor['sortname'];
endforeach;
$result = array_unique($shuzu);//是會默認返回下標從0開始的數組的
print_r($result);
?>

❼ 請問下Thinkphp5.0.5報未定義數組下標: 1是什麼原因呀該怎麼解決呀

把具有相同類型的若干變數按有序的形式組織起來的一種形式,這些按序排列的同類數據元素的集合稱為數組。

數組成員引用的下標值就是這個成員元素在數組中的位置。因此必須大於或等於1。

$POST['id']之所以報未定義索引是因為$POST這個數組里沒有包含下標鍵值為id的值。

數組的下標都是從0開始的

要記錄下標直接寫成

for(int i = 0;i<a.length;i++){//i就是下標,a.length就是數組長度

}

(7)php數組下標從1開始擴展閱讀:

ThinkPHP框架的文件全部採用UTF-8編碼格式,UTF-8的支持和自動輸出編碼轉換的功能讓頁面表現更加靈活。可以配置輸出的頁面編碼格式,如gb2312等(默認採用UTF-8輸出)。

系統根據配置文件中設置的編碼格式自動對頁面進行編碼轉換,支持iconv和mb_string兩種方式,為了提高效率,如果系統的模板編碼設置和輸出編碼設置項相同,則不會進行編碼轉換。ThinkPHP可以設置模板編碼、輸出編碼和資料庫編碼,並自動完成轉換工作,讓你的應用不再受編碼的苦惱。

閱讀全文

與php數組下標從1開始相關的資料

熱點內容
哪些雲伺服器可以搭建郵件伺服器 瀏覽:686
美國試禁忌當上演員 瀏覽:304
hqss/vod/index.asp 瀏覽:404
男主很帥的歐美大尺度電影 瀏覽:356
怎麼消除安卓的系統通知 瀏覽:478
程序員3萬4是稅後嗎 瀏覽:868
javagui窗口 瀏覽:522
風月片網站動漫 瀏覽:289
電影拉幫套 瀏覽:745
鴉王電影國語版 瀏覽:600
造機甲的黑科技小說 瀏覽:779
歐美在線觀看網站 瀏覽:385
python整蠱代碼大全 瀏覽:458
電影 中國 飛機 瀏覽:103
畫江湖推倒常宣靈小說 瀏覽:158
java表格居中 瀏覽:404
能來回穿梭現代和民國的小說 瀏覽:830
法國版未刪 瀏覽:755
java中字元串輸入 瀏覽:185
可愛女友糖糖圓圓小詩 瀏覽:272