當前位置:首頁 » 編程語言 » php數組的維度

php數組的維度

發布時間: 2022-06-14 08:41:22

A. php判斷一個多維數組當中有多少個n維數組

1、本次僅代表個人意見,不喜勿噴

2、以下代碼使用到的知識點包括,函數遞歸調用,數組去重,數組排序,以及數組遍歷

3、本次測試了三個例子,例子的結果見程序後附圖


/**************************** 代碼開始 begin*************************************/

<?php

/***
*@author biking
*@time 21015-11-18
*@function 獲取數組的維數
*/

function getArrayNum($array,$n,&$num){
if(!is_array($array)){
return ;
}

foreach($array as $val){
if(is_array($val)){
$tmpn = $n+1;
getArrayNum($val, $tmpn,$num);//遞歸調用
}else{
array_push($num,$n);
continue;
}
}
}

/************************測試例子*********************/

//$myarray = array(1,2,array(11,22),3,array(33,44,array(111,222,333)));//例子1
//$myarray = "hello";//例子2
$myarray = array(2,3,array('hei','this'));//例子3

$num = array();
getArrayNum($myarray, 1,$num);//首次調用

if(empty($num)){//進行判斷
echo "<meta charset='utf-8' />不是數組!";
die();
}

//數組去重
array_unique($num);

//升序排序
sort($num);

//輸出測試的數組的維數
echo "<meta charset='utf-8' />該例子的數組維數是:".$num[count($num)-1];


/**************************** 代碼結束 end*************************************/

例子1結果

B. php多維數組如何使用

$User[0][0]的值就是'張三'、$User[0][1]的值就是88;
$User[1][0]的值就是'lisi'、$User[1][1]的值就是90;
....

更高級的用法見下面的例子程序:
<?php
$User =array(
'張三' => 88,
'lisi' => 90,
'王五' => 99
);
print_r($User);
?>

這樣$User['張三']的值就是88,$User['lisi']的值就是90,是一維數組,而且更加方便。

C. php數組、鍵名、索引、鍵值有什麼區別

數組的根據類型來分,分為關聯數組和數字索引數組。

D. php中判斷數組是一維,二維,還是多維的解決方法

functionis_array($array){
$s=1;默認為1為數組
foreach($arrayas$value){
在這里判斷value是不是數組,是的話,說明是2維
設置$s=2;

}
return$s;
}

E. PHP多維數組

<?php
$arr = ['a','b',['c','d']];
foreach($arr as $i){
if(is_array($i)){
foreach($i as $j){
echo $j;
}
} else {
echo $i;
}
}
用is_array()函數判斷當前從數組中取到的元素是不是數組,如果是數組,就再加一層循環

F. php 一維數組、二維數組、多維數組區別詳解

簡單說說吧:

一維數組:[ 0 ]索引 =>指向 [ ... ]內容
array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ]}

二維數組:
array {
[ 0 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 1 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 2 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 3 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 4 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] }
}

三維數組:
array {
[ 0 ] =>
array {
[ 0 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 1 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 2 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 3 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 4 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] }
}
[ 1 ] =>
array {
[ 0 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 1 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 2 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 3 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 4 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] }
}
[ 2 ] =>
array {
[ 0 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 1 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 2 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 3 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 4 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] }
}
[ 3 ] =>
array {
[ 0 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 1 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 2 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 3 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 4 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] }
}
[ 4 ] =>
array {
[ 0 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 1 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 2 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 3 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] },
[ 4 ]=>array { [ 0 ]=>[ 內容 ],[ 1 ]=>[ 內容 ],[ 2 ]=>[ 內容 ],[ 3 ]=>[ 內容 ],[ 4 ]=>[ 內容 ] }
}
}

維數越多,嵌套越復雜,頭腦要清晰啊!

G. php如何判斷數組是幾維的

function is_array($array){ $s = 1;默認為1為數組 foreach($array as $value){ 在這里判斷value是不是數組,是的話,說明是2維 設置$s=2; }return $s;}

熱點內容
什麼是法人賬號密碼 發布:2025-07-15 10:34:59 瀏覽:874
編程題抽獎 發布:2025-07-15 10:34:00 瀏覽:629
linux手動編譯的內核怎麼刪 發布:2025-07-15 10:31:56 瀏覽:96
存儲行業發展趨勢 發布:2025-07-15 10:25:22 瀏覽:242
怎麼刪除配置提示 發布:2025-07-15 10:21:27 瀏覽:245
java深入學習 發布:2025-07-15 10:13:50 瀏覽:535
linux應用程序開發pdf 發布:2025-07-15 10:11:37 瀏覽:911
解壓冷知識 發布:2025-07-15 10:11:35 瀏覽:78
outlook郵件的伺服器是什麼 發布:2025-07-15 09:45:59 瀏覽:482
如何安排資產配置 發布:2025-07-15 09:33:24 瀏覽:906