當前位置:首頁 » 編程語言 » 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;}

熱點內容
mac運行fl需要什麼配置 發布:2025-02-07 08:15:45 瀏覽:572
安卓怎麼做都比不了蘋果 發布:2025-02-07 08:12:47 瀏覽:237
怎麼給物理機配置ip地址 發布:2025-02-07 08:01:37 瀏覽:138
三國志13未加密 發布:2025-02-07 07:54:37 瀏覽:925
馬斯克中國訪問 發布:2025-02-07 07:54:29 瀏覽:101
資料庫有表 發布:2025-02-07 07:50:49 瀏覽:28
基於nginx搭建圖片伺服器原理 發布:2025-02-07 07:44:18 瀏覽:448
java等待 發布:2025-02-07 07:28:24 瀏覽:612
vs編譯器會自己加空格嗎 發布:2025-02-07 07:23:05 瀏覽:175
光遇切換賬號安卓要輸入些什麼 發布:2025-02-07 07:10:20 瀏覽:501